主题:求助C++从文件中读取结构体数组的问题
{
char num[7];
char name[20];
float chinese;
float math;
float english;
};
void readfile(struct stu s[])//读文件
{
FILE *fp;
int c;
c = 0;
if ((fp = fopen("chengji", "rb")) == NULL)
{
printf( "can't open file\n");
exit(1);
}
printf("学号\t姓名\t语文\t数学\t英语\n");
while (!feof(fp))
{
fscanf(fp, "%s%s%f%f%f", &s[c].num, &s[c].name, &s[c].chinese, &s[c].math, &s[c].english);
printf("%s\t%s\t%f\t%f\t%f\n", s[c].num, s[c].name, s[c].chinese, s[c].math, s[c].english);
c++;
}
printf("共有%d条学生记录\n", c);
fclose(fp);
n = c;
}
void save(struct stu s[])//写入文件
{
if (n == 0)
cout << "没有记录可保存\n";
else
{
FILE *fp;
if ((fp = fopen("chengji", "wb")) == NULL)
{
cout<<"can't open file\n";
exit(1);
}
for (int i = 0; i < n; i++)
fprintf(fp, "%s%s%f%f%f", s[i].num, s[i].name, s[i].chinese, s[i].math, s[i].english);
fclose(fp);
//readfile(s);
}
}
请大家帮忙看下我的读文件和写文件有什么问题?谢谢了!