[問題] 關於write和read
小弟我使用的是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
01/23 17:51, 1F
→
01/23 17:54, , 2F
01/23 17:54, 2F
→
01/23 17:55, , 3F
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)
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):