include “stdafx.h”
include
#include <stdio.h>
#include <stdlib.h>
struct stu{
float num; //学号
int age; //年龄
};
int main()
{
stu gao;
gao.age = 12;
gao.num = 3.5;
FILE * pwFile;
if (( fopen_s(&pwFile,"myfile.txt", "wb")) != 0)
{
printf("cant open the file");
exit(0);
}
//可以写多个连续的数据(这里一次写4个)
fwrite(gao, sizeof(stu), 4, pwFile);
fclose(pwFile);
stu gaobak;
FILE * prFile;
if ((fopen_s(&prFile, "myfile.txt", "rb")) != 0)
{
printf("cant open the file");
exit(0);
}
fread(gaobak, sizeof(stu), 4, prFile);
std::cout << gaobak.age <<gaobak.num<<" "<< std::endl;
fclose(prFile);
while (1);
return 0;
}