今天看到网上一个乱七八糟的乱子,整理一下:
#include <stdio.h>
#include <string.h>
struct ST
{
int NO;
char name[16];
};
int main()
{
ST test;
ST read;
FILE *pFile;
test.NO = 1;
strcpy(test.name, “slackcode.cn”);
pFile = fopen(“file1.dat”, “w+”);
if ( !pFile )
{
printf(“open file error!”);
return -1;
}
rewind(pFile);
fwrite(&test<font col
or=”#000080”>, sizeof(ST), 1, pFile);
rewind(pFile);
fread(&read, sizeof(ST), 1, pFile);
printf(“%d, %s\n”, read.NO, read.name);
fclose(pFile);
return 0;
}
把结构体写入到文件
- 本文链接: http://imlcl.com/2008/02/08/write-the-struct-to-file/
- 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 许可协议。转载请注明出处!