文件操作(结构体)
將結(jié)構(gòu)體內(nèi)容寫入到文件中
#include<stdio.h> #include<string.h>struct student {char name[100];int age; };int main() {struct student st = {"wangqinghe",30};FILE * p = fopen("./c.txt","wb");fwrite(&st,sizeof(st),1,p);fclose(p);return 0; }寫入到文件中,文件大小是104b,(struct類的大小),多余的未填充的字段會是亂碼。
會將整個結(jié)構(gòu)大小和內(nèi)容寫入。
#include<stdio.h> #include<stdlib.h>struct student {char name[100];int age; };int main() {struct student st = {0};FILE *p = fopen("./c.txt","rb");fread(&st,sizeof(st),1,p);fclose(p);printf("name = %s, age = %d\n",st.name,st.age);return 0; }運(yùn)行結(jié)果:
exbot@ubuntu:~/wangqinghe/C/20190723$ gcc readWrite.c -o readWrite
exbot@ubuntu:~/wangqinghe/C/20190723$ ./readWrite
name = wangqinghe, age = 30
轉(zhuǎn)載于:https://www.cnblogs.com/wanghao-boke/p/11240352.html
總結(jié)
- 上一篇: 为什么lol那些女的,不会玩辅助还能躺着
- 下一篇: 文件操作(排序)