#include
#include
struct stu
{
float english;
float computer;
float math;
char name[20];
}a[20],*p;
int main()
{
FILE *fp1,*fp2;
float avg;
int i;
p=a;
if((fp1=fopen("file2.dat","rt"))==NULL)
{
printf("failed to open file2.dat!");
exit(1);
}
for(i=0;i fscanf(fp1,"%s%d%d%d\n",p->name,&p->english,&p->computer,&p->math);
p=a;
fclose(fp1);
if((fp2=fopen("file3.dat","wt"))==NULL)
{
printf("failed to open file3.dat!");
exit(1);
}
printf("个人平均分大于80分学生信息如下:\n");
printf("%-8s%-8s%-8s%-8s\n","姓名","英语","计算机","数学");
for(i=0;i {
avg=(p->english+p->english+p->math)/3;
if(avg>=80)
{
printf("%-8.2f%-8.2f%-8.2f\n",p->english,p->computer,p->math);
fprintf(fp2,"%-8.2f%-8.2f%-8.2f\n",p->english,p->computer,p->math);
}
}
fclose(fp2);
return 0;
}
fscanf这里为什么我无法写入数组,弄了一个下午,头都晕了
q 11,22,33
w 44,55,66
e 45,32,45
r 78,55,69
t 78,55,87
y 23,45,98
u 12,56,88
i 89,65,32
o 98,44,55
p 65,32,12
a 12,45,56
s 88,99,56
d 56,78,56
f 45,78,56
g 56,55,78
h 56,77,89
j 56,55,78
k 56,56,88
l 22,33,55
z 45,55,66这是我的file1.dat文件
是file2.dat打错了
file2.dat我是这么写入的
fprintf(fp,"%s\t%d,%d,%d\n",student[i].name,student[i].english,student[i].computer,student[i].math);
fclose(fp);
按照下面改一下试试。
while (fscanf(fp1,"%s%d%d%d\n",p->name,&p->english,&p->computer,&p->math) == 4) {
p++;
}