[問題] 關於 argc 和 argv[]

看板C_and_CPP作者 (Killen)時間10年前 (2013/10/27 17:36), 編輯推噓0(007)
留言7則, 3人參與, 最新討論串1/1
開發平台(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
就是 argc != 3 的意思,copyFile file1 file2
10/27 18:22, 1F

10/27 18:43, , 2F
那請樓上解釋一下argc == 2時會如何?
10/27 18:43, 2F

10/27 18:44, , 3F
第二譴責原po隨便找code來套 第三是都會google了那搜尋
10/27 18:44, 3F

10/27 18:44, , 4F
一下argc應該沒有任何困難吧?
10/27 18:44, 4F

10/27 20:01, , 5F
http://ppt.cc/iYfv 第零點看到第參點
10/27 20:01, 5F

10/27 20:15, , 6F
不過仔細看一下 code, 這範例還真的頗..特別.
10/27 20:15, 6F

10/27 23:51, , 7F
喔喔,我是看他的錯誤訊息猜的,是這 code 寫不好
10/27 23:51, 7F
文章代碼(AID): #1IRDw5F2 (C_and_CPP)