[問題] argc問題
這是一個用來 copy 一檔案 至 另一檔案程式,我complier後會產生一個Projec.exe
在MS-DOS模式下,鍵入如下指令: Project.exe a.txt b.txt 即可將a的內容複製到b
問題是:是否可以在另一個程式直接呼叫我的Proect.exe就可以產生同樣功能?
#include <stdio.h>
#include <stdlib.h>
void main(int argc, char *argv[])
{
char c;
int toScreen = 1;
FILE *fpin, *fpout;
if(argc < 2 || argc > 3)
{ printf("The correct format is: copyFile file1 file2\n");
exit(1);
}
fpin = fopen(argv[1], "r");
if( !fpin )
{ printf("The file: %s is not found!\n", argv[1]);
return;
}
if(argc == 3)
{ fpout = fopen(argv[2], "w");
if( !fpout )
{ printf("The file: %s is not found, or no available space!\n", argv[2]);
return;
}
toScreen = 0;
}
while( (c=getc(fpin)) != EOF)
{ if( toScreen )
putchar(c);
else
putc(c, fpout);
}
fclose(fpin);
if( !toScreen)
fclose(fpout);
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.210.112.66
→
10/29 14:25, , 1F
10/29 14:25, 1F
推
10/29 14:32, , 2F
10/29 14:32, 2F
→
10/29 14:33, , 3F
10/29 14:33, 3F
→
10/29 14:39, , 4F
10/29 14:39, 4F
→
10/29 15:17, , 5F
10/29 15:17, 5F
→
10/29 15:19, , 6F
10/29 15:19, 6F
推
10/29 15:27, , 7F
10/29 15:27, 7F
→
10/29 15:28, , 8F
10/29 15:28, 8F
→
10/29 15:28, , 9F
10/29 15:28, 9F
→
10/29 15:52, , 10F
10/29 15:52, 10F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章