返回信息流#include <stdio.h>
struct student{
char name[30];
int id;
double politics;
double english;
double math;
double special;
double cheng_ji;
};
int main(void)
{
struct student s[5];
int i;
FILE *fp1,*fp2;
fp1=fopen("score.txt","r");
fp2=fopen("sum.txt","w");
for(i=1;i<5;i++)
{
fscanf(fp1,"%s",s[i].name);//此处没有加&,加上也没错,
fscanf(fp1,"%d",&s[i].id);//以下的都加,不加会出错,不知原因
fscanf(fp1,"%lf",&s[i].politics);
fscanf(fp1,"%lf",&s[i].english);
fscanf(fp1,"%lf",&s[i].math);
fscanf(fp1,"%lf",&s[i].special);
s[i].cheng_ji=s[i].english+s[i].math+s[i].politics+s[i].special;
}
fprintf(fp2,"name\t\tid\tpol\teng\tmat\tspe\tche\n");
for(i=0;i<5;i++)
{
fprintf(fp2,"%s\t",s[i].name);
fprintf(fp2,"%d\t",s[i].id);
fprintf(fp2,"%lf\t",s[i].politics);
fprintf(fp2,"%lf\t",s[i].english);
fprintf(fp2,"%lf\t",s[i].math);
fprintf(fp2,"%lf\t",s[i].special);
fprintf(fp2,"%lf\n",s[i].cheng_ji);
}
}
1:在注释
2:编译无错,运行时出错:File:fscanf.c Line:54 Expression:stream!=NULL
谢谢
这是一条镜像帖。来源:北邮人论坛 / cpp / #9162同步于 2008/7/4
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
请教个程序,
abcxyz6688
2008/7/4镜像同步5 回复
订阅后,新回复会通过你的通知中心匿名送达。
5 条回复
1:建议你翻翻课本,看看指针和基本变量得区别,和standard io
【 在 abcxyz6688 (abcxyz6688) 的大作中提到: 】
: #include <stdio.h>
: struct student{
: char name[30];
: ...................
我注意到你的两个for循环不对称,看你程序的大概意思应该是读出5个数据,然后输出到文件吧;
for(i=1;....
for(i=0;....
第一个for语句应该改为 i=0;i<5;i++
【 在 abcxyz6688 (abcxyz6688) 的大作中提到: 】
: #include <stdio.h>
: struct student{
: char name[30];
: ...................
re
夫祸患常积于忽微
一些不起眼得小bug,很可能就是原因。。。。
【 在 abcxyz6688 (abcxyz6688) 的大作中提到: 】
: name变量是字符数组,故不用&
: i=0,是不对称,谢了