首页 新闻 会员 周边

我用指针为什么总出错

0
悬赏园豆:50 [已解决问题] 解决于 2013-01-02 18:13

#include<stdio.h>
#include<string.h>
struct student
{
 long num;
 char name[10];
 int eng;
 int math;
 int comp;
 char addr[20];
}stu[10];
int main()
{
    int n;
 void fail(struct student *p);
   struct student *q;
 q=stu;
 
 printf("please choose 1,2or3:");
 scanf("%d",&n);

fail(stu);
  return 0;
}
#include<stdlib.h>
void fail(struct student *p)
{
 FILE *fp,*fpp;int sum,i;char c[50];
 fp=fopen("D:\\ccc\\file1.txt","r");
 fpp=fopen("D:\\ccc\\file2.txt","w");
 if(fp==NULL||fpp==NULL)
 {
  printf("can't open file!\n");
  exit(0);
 }
 printf("姓名  总成绩  地址\n");fgets(c,45,fp);
 for(i=0;i<10;i++,p++)
 {
  
  fscanf(fp,"%d%d%d",p.eng,p.math,p.comp);//这句总出错
  sum=(*p).eng+(*p).math+(*p).comp;
  if(sum<180)
   printf("%c,%d,%c\n",(*p).name,sum,(*p).addr);
  
 }
}

笨蛋001的主页 笨蛋001 | 初学一级 | 园豆:157
提问于:2012-12-31 18:39
< >
分享
最佳答案
2

fscanf(fp,"%d%d%d", &(p->eng),&(p->math),&(p->comp));

另外代码能不能写的规范一点:

 FILE *fp,*fpp;int sum,i;char c[50];

分成三行些可以吧

收获园豆:40
冷暖自知01 | 菜鸟二级 |园豆:242 | 2013-01-01 23:47
其他回答(1)
-1

fscanf(fp,"%d%d%d",p->eng,p->math,p->comp);

因为那是指针,要用->

收获园豆:10
iddmx | 园豆:208 (菜鸟二级) | 2013-01-01 20:30
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册