fread fwrite 的使用
2012-05-01 22:03
302 查看
下面是一个用fread,fwrite向结构体中写入结构体,读出结构体的代码:
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include<string.h> struct stu { char name[20]; char sex[20]; int age; }; int save() { struct stu mystu[3]; FILE *fp; extern int errno; char fpath[]="record"; int i; // memset(mystu,0,sizeof(mystu)); strcpy(mystu[0].name,"yang你的"); printf("%s\n",mystu[0].name); strcpy(mystu[0].sex,"yang方法"); mystu[0].age=23; strcpy(mystu[1].name,"cai方法"); strcpy(mystu[1].sex,"cai读的"); mystu[1].age=21; strcpy(mystu[2].name,"liu的"); strcpy(mystu[2].sex,"liu"); mystu[2].age=20; fp=fopen(fpath,"w"); if(fp==NULL) { printf("can't open the file !"); printf("errno:%d",errno); printf("ERROR :%s",strerror(errno)); return 0; } else { printf("The file is opened"); } i=fwrite(mystu,sizeof(mystu),1,fp); printf("%d bit was written.\n",i); fclose(fp); return 0; } int load() { struct stu mystu[3]; FILE *fp; extern int errno; char fpath[]="record"; int i; fp=fopen(fpath,"rb"); if(fp==NULL) { printf("can't open the file !"); printf("errno:%d",errno); printf("ERROR :%s",strerror(errno)); return 0; } else { printf("The file is opened"); } i=fread(mystu,sizeof(mystu),1,fp); printf("%d bit was read.\n",i); int j; for(j=0;j<3;j++) { printf("%s %s %d\n",mystu[j].name,mystu[j].sex,mystu[j].age); } fclose(fp); return 0; } int main() { // save(); load(); return 0; }
相关文章推荐
- [Linux流操作]使用fread和fwrite函数读写文件
- fseek,fwrite,fread使用
- VC中fread,fwrite,fseek,ftell的使用
- linux下标准I/O的使用——fread/fwrite
- 文件块读写-使用fread和fwrite
- 如何使用Fread和Fwrite写一个复制文件的函数
- 问题点&二进制I/O函数fread(),fwrite()的使用
- 使用fread和fwrite函数读写文件
- fread和fwrite的使用举例
- fread和fwrite的使用
- fwrite()、fread()函数使用小结
- fopen()、fwrite()、fread()函数使用说明与示例
- fopen()、fwrite()、fread()函数使用说明与示例
- fread(从文件读取)和fwrite(向文件写入)配合使用
- fopen()、fwrite()、fread()函数使用说明与示例
- fread和fwrite使用的题..
- fopen\fread\fwrite\fscanf\fprintf\fseek\feof\rewind\fgets\fputc等系列函数使用总结
- fopen\fread\fwrite\fscanf\fprintf\fseek\feof\rewind\fgets\fputc等系列函数使用总结
- strtok()函数、fseek()函数、fwrite()函数、fread()函数的使用
- Linux C中read/write,fread/fwrite等的使用说明!