我服了这论坛了,我发了3天没人教,我去别的论坛2小时就搞定了
这是他们教我 用fgets和sscanf搞的
#include<stdio.h>
#include<malloc.h>
#include<string.h>
struct student
{
int no;
char name[20];
int cpp;
int math1;
int english;
int math2;
float evg;
int rank;
struct student *next;
};
main()
{
char s[1000];
struct student *p,*head,*last,*pw;
//.......
p=(struct student*) malloc(sizeof(struct student));
FILE *fp;
if((fp=fopen("d://c++6.0//t1.txt","r"))!=NULL)
{
fgets(s,1000,fp);
//吸收第一行表头
if(!feof(fp))
{
fgets(s,1000,fp);
sscanf(s,"%d%s%d%d%d%d",&p->no,p->name,&p->cpp,&p->math1,&p->english,&p->math2);
p->evg=(p->cpp+p->math1+p->english+p->math2)/4.0;
p->next=NULL;
last=p;
head=p;
while(!feof( fp ))
{
p=(struct student*) malloc(sizeof(struct student));
fscanf(fp,"%d%s%d%d%d%d",&p->no,p->name,&p->cpp,&p->math1,&p->english,&p->math2);
p->evg=(p->cpp+p->math1+p->english+p->math2)/4.0;
p->next=NULL;
last->next=p;
last=p;
}
}
else
printf("文件内无学生信息");
}
else
printf("打开文件失败");
}
[
本帖最后由 ccmike98 于 2010-7-27 00:29 编辑 ]