[問題] 關於write和read

看板C_and_CPP作者 (尖頭鰻)時間14年前 (2011/01/23 09:39), 編輯推噓0(004)
留言4則, 3人參與, 最新討論串1/2 (看更多)
小弟我使用的是dev c++ #include <stdio.h> #include <stdlib.h> #include <windows.h> #include <fcntl.h> #include <io.h> #include <sys/stat.h> struct super { int a1; int a2; int a3; int a4; int a5; int a6; }; struct super Test={1,2,3,4,5,6}; struct super Test2; int main() { int f,f2; f=open("c:\\123\\test.bin",O_CREAT|O_RDWR); if(f==-1) { printf("test.bin檔案開啟失敗"); system("pause"); } else { write(f,&Test,sizeof(Test)); close(f); } f2=open("c:\\123\\test.bin",O_CREAT|O_RDWR); if(f2==-1) { printf("test.bin檔案開啟失敗") system("pause"); } else { read(f2,&Test2,sizeof(Test2)); close(f2); printf("%d %d %d %d %d %d\n",Test2.a1,Test2.a2,Test2.a3,Test2.a4,Test2.a5,Test2.a6); system("pause"); } return 0; } 我想測試write成bin檔後再read出來 把Test的內容存進一個test.bin 再讀出來給Test2 請問一下各位大大..為何這樣秀出來的值會是六個0呢? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.46.151.214

01/23 17:51, , 1F
你有檢查過c:\\123\\test.bin這個檔案的內容嗎?
01/23 17:51, 1F

01/23 17:54, , 2F
怎麼不檢查write跟read的回傳值???
01/23 17:54, 2F

01/23 17:55, , 3F
f跟f2的值也都沒檢查,這樣寫程式太隨便了吧
01/23 17:55, 3F

01/23 17:58, , 4F
我只是寫個測試而已..回傳直我看過~沒問題
01/23 17:58, 4F
※ 編輯: SuperEel 來自: 114.46.151.214 (01/23 18:25) ※ 編輯: SuperEel 來自: 114.46.151.214 (01/23 18:42)
文章代碼(AID): #1DE_TOqT (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1DE_TOqT (C_and_CPP)