一个文件(book.txt)
一个类book,有成员id,name,author等等
怎么将文件中的数据信息导入到类中
,求求大佬救命,孩子想了仨小时了,实在是不会
给你看下这个例子吧。
struct Student
{
int nYear;
char czName[20];
char czNumber[10];
char czTel[12];
int nAge;
float fScore[5];
}Student[10];
int main()
{
int nCount = 0;
FILE *fp = fopen("22.txt","rb");
if(fp == NULL)
{
printf("can not open file!\n");
exit(0);
}
for(int i = 0;i < 100;i ++)
{
int nRes = fscanf(fp,"%d %s %s %s %d %f %f %f %f %f",&Student[i].nYear,Student[i].czName,Student[i].czNumber,Student[i].czTel,
&Student[i].nAge,&Student[i].fScore[0],&Student[i].fScore[1],&Student[i].fScore[2],&Student[i].fScore[3],&Student[i].fScore[4]);
if (nRes == -1)
{
nCount = i;
fclose(fp);
break;
}
}
for (int i = 0;i < nCount;i ++)
{
printf("%d %s %s %s %d %.2f %.2f %.2f %.2f %.2f\n",Student[i].nYear,Student[i].czName,Student[i].czNumber,Student[i].czTel,
Student[i].nAge,Student[i].fScore[0],Student[i].fScore[1],Student[i].fScore[2],Student[i].fScore[3],Student[i].fScore[4]);
}
fclose(fp);
return 0;
}