#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.00tom2,1002,95.00,92.00,93.00,93.33tom3,1003,95.00,96.00,94.00,95.00tom4,1004,95.00,92.00,94.00,93.67tom5,1005,94.00,91.00,98.00,94.33
1 回答
蛊毒传说
TA贡献1895条经验 获得超3个赞
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
可以使用
atoi
例如:
char
num=
"456"
;
int
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);
- 1 回答
- 0 关注
- 62 浏览
添加回答
举报
0/150
提交
取消