[問題] 關於 argc 和 argv[]
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
CPP
問題(Question):
想請問,以下是一個能copy一個txt文字檔到另一個文字檔的程式碼
我本來是使用
fpin = fopen("file1.txt", "r");
fpout = fopen("file2.txt", "w");
這樣來開啟兩個檔案
但是在網路搜尋的結果,發現有以下的方式,但我執行之後馬上就會跳到
exit(1);
,想請問一下
if(argc < 2 || argc > 3)
這一行是什麼意思呢??,要如何正確的COPY檔案,謝謝!!!
程式碼(Code):(請善用置底文網頁, 記得排版)
#include <stdio.h>
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 0;
}
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 0;
}
toScreen = 0;
}
while( (c=getc(fpin)) != EOF)
{ if( toScreen )
putchar(c);
else
putc(c, fpout);
}
fclose(fpin);
if( !toScreen)
fclose(fpout);
system("pause");
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.113.76.101
→
10/27 18:22, , 1F
10/27 18:22, 1F
→
10/27 18:43, , 2F
10/27 18:43, 2F
→
10/27 18:44, , 3F
10/27 18:44, 3F
→
10/27 18:44, , 4F
10/27 18:44, 4F
→
10/27 20:01, , 5F
10/27 20:01, 5F
→
10/27 20:15, , 6F
10/27 20:15, 6F
→
10/27 23:51, , 7F
10/27 23:51, 7F