[問題] 關於檔案處理的問題
嗯...自修看到一半也是發現範例的題目有點看不懂。
先把範例程式碼附上:
1) #include <stdio.h>
2) #include <stdlib.h>
3) #include <fcntl.h>
4) #include <io.h>
5) #include <sys/stat.h>
6) #define SIZE 512
7) int main(void)
8) {
9) char buffer[SIZE];
10) int f1,f2,bytes;
11)
12) f1=open("example.txt",O_RDONLY|O_TEXT);
13) f2=creat("output1.txt",S_IWRITE);
14)
15) if((f1!=-1)&&(f2!=-1))
16) {
17) while(!eof(f1))
18) {
19) bytes=read(f1,buffer,SIZE);
20) write(f2,buffer,bytes);
21) }
22) close(f1);
23) close(f2);
24) printf("檔案複製完成!!\n");
25) }
26) else
27) printf("檔案開啟失敗!!\n");
30)
29) system("pause");
30) return 0;
31) }
問題:
(1)第19行我理解的是從f1(example.txt)讀取,一次讀取
SIZE個位元組個資料,在存放至buffer陣列裡,在設定給
Bytes,此時bytes的內容是什麼阿?我怎麼感覺是資料...
另外在19行跟20行間加入printf("%d\n",bytes);,發現
每次都沒有讀取SIZE個,怎麼跟設定的不一樣呢?
(2)把第19行的SIZE改為999或是888為什麼就無法執行了
(3)如果把第6行的SIZE定義為0,第19行的SIZE更改為1,
為什麼一樣可以複製?buffer陣列是0耶.....
抱歉,因為真的搞不懂又想很久都想不到才來發問。
謝謝
※ 編輯: elohacp4 (111.251.5.212), 05/02/2015 00:05:17
→
05/02 02:07, , 1F
05/02 02:07, 1F
大哥,幫忙一下拜託你了,求你!!!
※ 編輯: elohacp4 (114.25.153.133), 05/02/2015 08:27:58
推
05/02 08:59, , 2F
05/02 08:59, 2F
請問意思是再去翻函數這章來看是嗎?
※ 編輯: elohacp4 (42.75.243.8), 05/02/2015 09:43:34
→
05/02 10:42, , 3F
05/02 10:42, 3F
→
05/02 15:59, , 4F
05/02 15:59, 4F
→
05/02 21:26, , 5F
05/02 21:26, 5F
→
05/02 21:26, , 6F
05/02 21:26, 6F
→
05/02 21:26, , 7F
05/02 21:26, 7F
→
05/02 21:28, , 8F
05/02 21:28, 8F
→
05/02 22:32, , 9F
05/02 22:32, 9F
→
05/02 22:33, , 10F
05/02 22:33, 10F
→
05/02 23:01, , 11F
05/02 23:01, 11F
→
05/02 23:02, , 12F
05/02 23:02, 12F
→
05/02 23:03, , 13F
05/02 23:03, 13F