通过一个例子来熟悉使用三个函数
fputc函数原型int fputc(int c, FILE *stream);
#include#include int main() { FILE *fp; int i; char *str="I Love you the way you are!n"; fp=fopen("./test.txt","w+"); int len=strlen(str); for(i=0;i fgetc与feof函数原型 int fgetc(FILE *stream);
int feof(FILE *stream);
#include补充:写结构体数组到文件中#include int main() { FILE *fp; char c; fp = fopen("./test.txt","r"); //当feof的返回值为非0值说明到达文件末尾了 while(!feof(fp)) { c = fgetc(fp); printf("%c",c); } fclose(fp); return 0; } #include#include #include #include #include #include struct Test { int a; char c; }; int main() { FILE *fp; struct Test data[2]={{100,'a'},{101,'c'}}; struct Test data2[2]; fp=fopen("./file1","w+"); int n_write=fwrite(&data,sizeof(struct Test)*2,1,fp); fseek(fp,0,SEEK_SET); int n_read=fread(&data2,sizeof(struct Test)*2,1,fp); printf("read %d,%cn",data2[0].a,data[0].c); printf("read %d,%cn",data2[1].a,data[1].c); fclose(fp); return 0; }



