结构体写入读出

在main方法里面,前半段注释了的是写入文件的代码,后半段是将写入的代码读出。

要注意两点:

第一,文件路径要写对,斜线用转义字符\\,否则会报出Debug inssertion failed

第二,在你将结构体写入的时候,查看文件可以会使乱码,因为他是以byte的形式写入的,别奇怪,只要能正确读出来就可以了。 当然如果你想要正确显示字符,还是改变一下解码方式吧。

/*Global inclusions*/

#include <stdio.h>

#include <stdafx.h>

#include <string.h>

#include <conio.h>

typedef struct ty_item

{

char iname[15];

int u_price;

int quan;

};

/*Main function*/

void main()

{

///*Define item structure*/

//ty_item t;

///*Define a file pointer*/

//FILE *fl;

//

//printf("Enter the intem details:\n");

///*Input data*/

//scanf("%s%d%d", t.iname, &t.u_price, &t.quan);

//

///*Open file*/

//fl = fopen("C:\\Documents\\Demo3.txt", "wb");

//fwrite(&t, sizeof(t), 1, fl);

//fclose(fl);

FILE *fl;

char ch;

ty_item t;

fl = fopen("C:\\Users\\I075181\\Documents\\Demo3.txt", "rb");

fread(&t, sizeof(t), 1, fl);

printf("\n");

printf("%s\t\%d\t%d\n", t.iname, t.u_price, t.quan);

fclose(fl);

getch();

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值