c语言对结构体的读取与写入
来源:互联网 发布:java h5微信支付demo 编辑:程序博客网 时间:2024/06/05 06:01
#include <stdio.h>#define SAVE_PATH "./student.data"typedef struct student{char name[10];int id;char addr[20];}student;void write_msg(const student* s,int len){#define _BIN_WRFILE* fp;int i = 0;#ifdef _BIN_WRfp = fopen(SAVE_PATH,"rb+");if(NULL == fp){fp = fopen(SAVE_PATH,"wb+");}fseek(fp,0,SEEK_END);while(i<len){fwrite(s+i,sizeof(student),1,fp);i++;}#elsefp = fopen(SAVE_PATH,"r+");if(NULL == fp){fp = fopen(SAVE_PATH,"w+");}fseek(fp,0,SEEK_END);while(i<len){fprintf(fp,"%-7s%-3d%-7s\n",(s+i)->name,(s+i)->id,(s+i)->addr);i++;}#endiffclose(fp);}void read(){FILE* fp;student s;#ifdef _BIN_WRfp =fopen(SAVE_PATH,"rb");if(NULL == fp){perror("open fail");return;}while(1==fread(&s,sizeof(student),1,fp)){printf("%s %d %s\n",s.name,s.id,s.addr);}#elsefp = fopen(SAVE_PATH,"r");if(NULL == fp){perror("open fail");}while(!feof(fp)){fscanf(fp,"%s %d %s\n",s.name,&s.id,s.addr);printf("%s %d %s\n",s.name,s.id,s.addr);}#endiffclose(fp);}int main(){student s[2]={{"ylk",111,"wuhan"},{"ylk2",222,"wuhan"}};write_msg(s,2);read();return 0;}
阅读全文
0 0
- c语言对结构体的读取与写入
- 【C#】对文本、图片、音频文件的读取与写入
- C语言的文件读取与写入操作
- C语言/C++文件的读取与写入操作
- 对文件的读取与写入
- c语言中往缓存写入结构体的方法
- C语言文件写入结构体的乱码解决方法
- C语言中结构体写入文件中,再从文件中读取,更新文件等
- C语言中将结构体写入文件
- C语言中将结构体写入文件
- C语言中将结构体写入文件
- c#excel文件的读取与写入
- 【C++】文件的读取与写入
- 【C#】关于文件的写入与读取
- 【C++】文件的读取与写入
- matlab写入二进制文件与C语言读取二进制文件
- C语言和C++的文件读取、写入函数
- C语言中打开文件读取,写入的操作
- 假设检验(z-test)
- Springboot 之 自定义配置文件及读取配置文件
- [来源未知][树上sa]旅行路线
- HDU1671 字典树
- The Suspects POJ
- c语言对结构体的读取与写入
- Day21
- 加农炮 51Nod
- 地址解析与逆解析
- CSU 1505 酷酷的单词_水题
- sentencepiece分词效果测试
- HDU 6115 Factory LCA求树上距离
- 线程锁_互斥量
- Fibonacci