关于C中读取文件的问题

youhaodeyi youhaodeyi 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我首先把一个结构写入到文件中,然后从文件中读取这个结构,如下:
1 #include "malloc.h"
2 #include "stdio.h"
3 typedef struct {
4 int numberint;
5 double numberdouble;
6 }Type;
7
8 int main()
9 {
10 FILE *file;
11 Type *t,*r;
12
13
14 t=(Type*)malloc(sizeof(Type));
15 t->numberint=10;
16 t->numberdouble=10.3838;
17
18 file=fopen("test.txt","w+");
19 if(file != NULL){
20 fwrite((const void*)(t),sizeof(Type),1,file);
21 close(file);
22 }
23
24 file=fopen("test.txt","r");
25 r = (Type*)malloc(sizeof(Type));
26 if(file!=NULL){
27 fread(r,sizeof(Type),1,file);
28 close(file);
29 printf("int:%d\n",r->numberint);
30 printf("double:%lf\n",r->numberdouble);
31 }
32 }

但是输出都是0,这是为什么?我在linux上运行,用的是gcc编译器
...全文

相关推荐

评论 3
收藏 已收藏
分享
返回顶部
youhaodeyi
C语言 70,039 成员 243,237 内容