- 论坛徽章:
- 0
|
另外贴一段代码:
#include <stdio.h>
int main(int argc, char *argv[])
{
struct data
{
float a;
char b;
char c;
char d;
char e;
};
struct data tmp = {0.1,'a','b','c','\n'};
FILE *fp = fopen("test.dat", "wb");
fwrite(&tmp,sizeof(tmp),1,fp);
printf("%f\n%c\n%c\n%c\n%c\n",tmp.a,tmp.b,tmp.c,tmp.d,tmp.e);
struct data tmp2;
fclose(fp);
FILE *fp2 = fopen("test.dat", "rb");
fread(&tmp2,sizeof(tmp2),1,fp2);
printf("%f\n%c\n%c\n%c\n%c\n",tmp2.a,tmp2.b,tmp2.c,tmp2.d,tmp2.e);
}
将0.1以写入文件,文件的内容以16进制查看 CD CC CC 3D 61 62 63 0A
再将文件的内容读入struct内,同样可以打印出0.1 a b c 和换行
printf函数会做近似操作? |
|