1、写
#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct But
{
int Num;
char name[10];
double high;
short age;
};
int main()
{
struct But b = { 123,"mw",180,22 };
FILE* pFile = fopen("C:\\Users\\lucky\\Desktop\\22.txt", "w");
fwrite(&b, sizeof(b), 1, pFile);
fclose(pFile);
system("pause");
return 0;
}
2、读
#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct But
{
int Num;
char name[10];
double high;
short age;
};
int main()
{
struct But b = { 123,"mw",180,22 };
struct But c;
FILE* pFile = fopen("C:\\Users\\lucky\\Desktop\\22.txt", "r");
fread(&c, sizeof(c), 1, pFile);
fclose(pFile);
system("pause");
return 0;
}
本文介绍了一个使用C语言进行结构体变量的文件存储与读取的例子。通过定义一个包含整型、字符数组、浮点型及短整型的结构体,并演示了如何将该结构体实例写入文件以及从文件中读取出来。
2053

被折叠的 条评论
为什么被折叠?



