为什么会显示无法正确读入呢?

#include<stdio.h>
struct student
{
char name[10];
int num;
float math;
float chinese;
float english;
float average;
};
main()
{
struct student stu[5];
int i;
FILE *fp;
if((fp = fopen("E:\\vc++6.0\\MSDev98\\MyProjects\\文件3\\stud.txt","r"))==NULL)
{
printf("the file opening error!!");
}
else
{
for(i = 0;i < 5;i++)
{
fscanf(fp,"%s,%d,%f,%f,%f,%f",stu[i].name,&stu[i].num,&stu[i].chinese,&stu[i].english,&stu[i].math,&stu[i].average);

}
fclose(fp);
for(i = 0;i < 5;i++)
{
printf("%s\n",stu[i].name);
}

}
}
tom1,1001,94.00,95.00,96.00,95.00
tom2,1002,95.00,92.00,93.00,93.33
tom3,1003,95.00,96.00,94.00,95.00
tom4,1004,95.00,92.00,94.00,93.67
tom5,1005,94.00,91.00,98.00,94.33

红颜莎娜
浏览 63回答 1
1回答

蛊毒传说

fscanf(fp,"%s,%d,%f,%f,%f,%f",stu[i].name,&stu[i].num,&stu[i].chinese,&stu[i].english,&stu[i].math,&stu[i].average);读取时候都要用%s,不然你读取的就有问题,如果想拿字符串转变成int&nbsp;&nbsp;可以使用atoi&nbsp;&nbsp;例如:char&nbsp;&nbsp;num="456";int&nbsp;a=atoi(num)a就是456了atof(将字符串转换成浮点型数)atoi(将字符串转换成整型数)atol(将字符串转换成长整型数)strtod(将字符串转换成浮点数)改为:后面的变量自己,转换之后重新给结构体赋值fscanf(fp,"%s,%s,%s,%s,%s,%s\n",stu[i].name,&stu[i].num,&stu[i].chinese,&stu[i].english,&stu[i].math,&stu[i].average);
打开App,查看更多内容
随时随地看视频慕课网APP