首页 新闻 会员 周边

怎么读入文本中的内容

0
悬赏园豆:20 [已解决问题] 解决于 2013-01-02 19:48

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct student
{
 long num;
 char name[10];
 int eng;
 int math;
 int comp;
 char addr[20];
};
struct student stu[10],*p=stu;
int main()
{
  void jf(struct student [],int n);
  void studentaver(struct student stu[],int n);
  void courseaver_max(struct student stu[],int n);
 
  jf(p,10);
  studentaver(p,10);
  courseaver_max(p,10);
  return 0;
}
#include<stdlib.h>
  void jf(struct student [],int n)
{
 int s[10],i;
 FILE*fp;
 fp=fopen("D:\\CCC\\file.txt","a+");
  if((fp=fopen("file.txt","a+"))==NULL)
  {
   printf("cannot open this file\n");
   exit(0);
  }
 printf("家访同学信息如下:\n");
 for(i=0;i<10;i++)
 {
  fscanf(fp,"%ld%s%d%d%d%s",&stu[i].num,stu[i].name,&stu[i].eng,&stu[i].math,&stu[i].comp,stu[i].addr);
        s[i]=stu[i].eng+stu[i].math+stu[i].comp;
  if(s[i]<180)
  {
   printf("学号 姓名 英语 数学 计算机 住址\n");
   printf("%ld%s%d%d%d%s\n",stu[i].num,stu[i].name,stu[i].eng,stu[i].math,stu[i].comp,stu[i].addr);
 }
}
 fclose(fp);
}
#include<stdlib.h>
void studentaver(struct student stu[],int n)
{
 int stuaver[10];
 int i;
 FILE*fp;
 fp=fopen("D:\\CCC\\file.txt","a+");
  if((fp=fopen("file.txt","a+"))==NULL)
  {
   printf("cannot open this file\n");
   exit(0);
  }
 printf("学号  姓名  平均分\n");
    for(i=0;i<10;i++)
 {
  stuaver[i]=int((stu[i].eng+stu[i].math+stu[i].comp)/3+0.5);
  fprintf(fp,"%d%s%f\n",stu[i].num,stu[i].name,stuaver[i]);
 }
  fclose(fp);
}
#include<stdlib.h>
void courseaver_max(struct student stu[],int n)
{
 int max[3]={0},i;
 float courseaver[3]={0.0};
 FILE*fp;
 fp=fopen("D:\\CCC\\file.txt","a+");
  if((fp=fopen("file.txt","a+"))==NULL)
  {
   printf("cannot open this file\n");
   exit(0);
  }
 printf("课程  平均分  最高分\n");
 for(i=0;i<10;i++)
 {
  fscanf(fp,"%d%d%d",&stu[i].eng,&stu[i].math,&stu[i].comp);
 }
    for(i=0;i<10;i++)
 {
  max[1]=max[1]>stu[i].eng?max[1]:stu[i].eng;
  max[2]=max[2]>stu[i].math?max[2]:stu[i].math;
  max[3]=max[3]>stu[i].comp?max[3]:stu[i].comp;
 }
 for(i=0;i<10;i++)
 {
  courseaver[1]+=stu[i].eng;
  courseaver[2]+=stu[i].math;
  courseaver[3]+=stu[i].comp;
 }
 fprintf(fp,"英语\t%f\t%d\n",courseaver[1],max[1]);
 fprintf(fp,"数学\t%f\t%d\n",courseaver[2],max[2]);
 fprintf(fp,"计算机\t%f\t%d\n",courseaver[3],max[3]);
 fclose(fp);
}

 

 

 

 

 

 

 

 

 

 

文本内容如下:

mum     name       eng   math   comp     addr
95001   奥巴马     88    97     98      room201
95002   卡扎菲     67    55     62      room201
95003   sunh       87    87     86      room203
95004   zhaoht     55    66     45      room203
95005   zhangy     91    78     98      room203
95006   凤姐       77    90     91      room212
95007   芙蓉姐姐   94    92     90      room212
95008   liuq       89    92     93      room316
95009   songh      35    66     57      room317
95010   李刚       88    99     92      room319

zhengzhiqiang的主页 zhengzhiqiang | 初学一级 | 园豆:12
提问于:2013-01-02 15:52
< >
分享
最佳答案
0

 

你这份代码的输入是没有问题的,输入是 fp=fopen("D:\\CCC\\file.txt","a+");和fscanf(fp,"%d%d%d",&stu[i].eng,&stu[i].math,&stu[i].comp);语句做到的

  第一句意思是打开在d盘的文件夹下的file.txt文件

  第二句意思是从这个文件中读入数据,我相信你能看懂的。

也就是说,只要你把想要输入的东西,按照输入格式写在 file.txt里面就没问题了。

你的代码里面   第43和44行printf("学号 姓名 英语 数学 计算机 住址\n");
   printf("%ld%s%d%d%d%s\n",stu[i].num,stu[i].name,stu[i].eng,stu[i].math,stu[i].comp,stu[i].addr);

输出在了控制台上,而 65行和98、99、100行却又把数据输出到了file.txt中,你到底是要做什么?

而且很多东西不知道你为什么这么写,比如41行,为什么小于180输出呢?

再有吐槽一下你代码的缩进,实在是乱。。。

我稍微改了一下你的代码,输出是这个样子的:

由于敏感词汇不让发的原因,换成注释*

家访同学信息如下:
学号 姓名 英语 数学 计算机 住址
95004 zhaoht 55 66 45 room203
学号 姓名 英语 数学 计算机 住址
95009 songh 35 66 57 room317
学号 姓名 平均分
95001 奥巴马 0.000000
95002 卡扎菲 0.000000
95003 sunh 0.000000
95004 zhaoht 0.000000
95005 zhangy 0.000000
95006 注释4 0.000000
95007 注释5^ 0.000000
95008 liuq 0.000000
95009 songh 0.000000
95010 李刚 0.000000
课程 平均分 最高分
英语 95684.000000 95001
数学 822.000000 99
计算机 812.000000 98

很明显很多数据不合理,你慢慢修改吧,祝你成功

 

收获园豆:20
iddmx | 菜鸟二级 |园豆:208 | 2013-01-02 19:36

谢了,

我知道哪错了,题也没看清

zhengzhiqiang | 园豆:12 (初学一级) | 2013-01-02 19:43

@zhengzhiqiang: 没事,把分给我就行,哈哈

iddmx | 园豆:208 (菜鸟二级) | 2013-01-02 19:44
其他回答(2)
0

我晕,这是三块代码吗。。。

最佳损友。 | 园豆:63 (初学一级) | 2013-01-02 16:01
0

在你的main函数中,调用方法前加一句getchar();就可以了

伏草惟存 | 园豆:1420 (小虾三级) | 2013-01-02 16:05

补在那一句之前,麻烦说下,谢了

支持(0) 反对(0) zhengzhiqiang | 园豆:12 (初学一级) | 2013-01-02 16:09

@zhengzhiqiang: void jf(struct student [],int n);?

支持(0) 反对(0) zhengzhiqiang | 园豆:12 (初学一级) | 2013-01-02 16:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册