通过读取该文件显示到屏幕上来检查stud.txt文件中的数据
读取文件的时候有问题,可以帮忙看一下吗?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
char num[10];
char name[20];
int sub1;
int sub2;
int sub3;
int ave;
struct student *next;
};
struct student *head = NULL;
struct student *creat();//创建节点
struct student *save(struct student *head);//保存链表数据到文件
struct student *read();//从文件读取链表
void display_all();//显示所有学生信息
struct student *creat()//创建节点
{
struct student *p1,*p2;
p1=p2=(struct student*)malloc(sizeof(struct student));
printf("请输入学生学号 姓名 语文成绩 数学成绩 英语成绩\n");
scanf("%s %s %d %d %d",p1->num,p1->name,&p1->sub1,&p1->sub2, &p1->sub3);
p1->ave = (p1->sub1+p1->sub2+p1->sub3)/3;
fflush(stdin);
head=p1;
for(int i=1;i<3;i++)
{
p1=(struct student*)malloc(sizeof(struct student));
scanf("%s %s %d %d %d",p1->num,p1->name,&p1->sub1,&p1->sub2, &p1->sub3);
p1->ave = (p1->sub1+p1->sub2+p1->sub3)/3;
fflush(stdin);
p2->next=p1;
p2=p1;
}
p2->next=NULL;
display_all();
return head;
}
void display_all()//显示所有学生信息
{
struct student *p;
p=head;
if(p == NULL)
printf("无学生信息!\n");
else{
printf("\n学生学号 姓名 语文成绩 数学成绩 英语成绩 平均成绩:\n");
while(p != NULL)
{
printf("%s\t%s\t%d\t%d\t%d\t%d\n\n",p->num,p->name,p->sub1,p->sub2,p->sub3,p->ave);
p=p->next;
}
}
}
struct student *save(struct student *head)//保存链表数据到文件
{
struct student *p;
FILE *fp;
p=head;
if((fp = fopen("stud.txt","w")) == NULL)
printf("文件无法打开!\n");
else
fp=fopen("stu.txt","w");
while(p != NULL)
{
fprintf(fp,"%s\t%s\t%d \t%d\t%d\t%d\n",p->num,p->name,p->sub1,p->sub2,p->sub3,p->ave);
p=p->next;
}
fclose(fp);
printf("保存成功!\n");
return head;
}
struct student *read()//从文件读取链表
{
struct student *p1,*p2;
FILE *fp;
int flag = 1;
fp=fopen("stud.txt","r");
printf("学生学号 姓名 语文成绩 数学成绩 英语成绩 平均成绩:\n");
while(!feof(fp))
{
if(flag == 1) ///读取头
{
p1=(struct student *)malloc(sizeof(struct student));
head=p1;
p2=p1;
fscanf(fp,"%s %s %d %d %d %d\n",p1->num,p1->name,&p1->sub1,&p1->sub2,&p1->sub3,&p1->ave);
printf("%s\t%s\t%d\t%d\t%d\t%d\n",p1->num,p1->name,p1->sub1,p1->sub2,p1->sub3,p1->ave);
flag--;
}
else
{
p1=(struct student *)malloc(sizeof(struct student));
fscanf(fp,"%s %s %d %d %d %d\n",p1->num,p1->name,&p1->sub1,&p1->sub2,&p1->sub3,&p1->ave);
printf("%s\t%s\t%d\t%d\t%d\t%d\n",p1->num,p1->name,p1->sub1,p1->sub2,p1->sub3,p1->ave);
p2->next=p1;
p2=p1;
}
}
p2->next = NULL;
fclose(fp);
return head;
}
int main()
{
creat();
save(head);
read();
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
char num[10];
char name[20];
int sub1;
int sub2;
int sub3;
int ave;
struct student* next;
};
struct student* head = NULL;
struct student* creat();//创建节点
struct student* save(struct student* head);//保存链表数据到文件
struct student* read();//从文件读取链表
void display_all();//显示所有学生信息
struct student* creat()//创建节点
{
struct student* p1, * p2;
p1 = p2 = (struct student*)malloc(sizeof(struct student));
printf("请输入学生学号 姓名 语文成绩 数学成绩 英语成绩\n");
scanf("%s %s %d %d %d", p1->num, p1->name, &p1->sub1, &p1->sub2, &p1->sub3);
p1->ave = (p1->sub1 + p1->sub2 + p1->sub3) / 3;
fflush(stdin);
head = p1;
for (int i = 1; i < 3; i++)
{
p1 = (struct student*)malloc(sizeof(struct student));
scanf("%s %s %d %d %d", p1->num, p1->name, &p1->sub1, &p1->sub2, &p1->sub3);
p1->ave = (p1->sub1 + p1->sub2 + p1->sub3) / 3;
fflush(stdin);
p2->next = p1;
p2 = p1;
}
p2->next = NULL;
display_all();
return head;
}
void display_all()//显示所有学生信息
{
struct student* p;
p = head;
if (p == NULL)
printf("无学生信息!\n");
else {
printf("\n学生学号 姓名 语文成绩 数学成绩 英语成绩 平均成绩:\n");
while (p != NULL)
{
printf("%s\t%s\t%d\t%d\t%d\t%d\n\n", p->num, p->name, p->sub1, p->sub2, p->sub3, p->ave);
p = p->next;
}
}
}
struct student* save(struct student* head)//保存链表数据到文件
{
struct student* p;
FILE* fp;
p = head;
if ((fp = fopen("stud.txt", "w")) == NULL)
printf("文件无法打开!\n");
else
fp = fopen("stu.txt", "w");
while (p != NULL)
{
fprintf(fp, "%s\t%s\t%d \t%d\t%d\t%d\n", p->num, p->name, p->sub1, p->sub2, p->sub3, p->ave);
p = p->next;
}
fclose(fp);
printf("保存成功!\n");
return head;
}
struct student* read()//从文件读取链表
{
struct student* p1, * p2=new student;
FILE* fp;
int flag = 1;
if ((fp = fopen("stud.txt", "r")) == NULL)
printf("文件无法打开!\n");
else
fp = fopen("stu.txt", "r");
printf("学生学号 姓名 语文成绩 数学成绩 英语成绩 平均成绩:\n");
while (!feof(fp))
{
if (flag == 1) ///读取头
{
p1 = (struct student*)malloc(sizeof(struct student));
head = p1;
p2 = p1;
fscanf(fp, "%s %s %d %d %d %d\n", p1->num, p1->name, &p1->sub1, &p1->sub2, &p1->sub3, &p1->ave);
printf("%s\t%s\t%d\t%d\t%d\t%d\n", p1->num, p1->name, p1->sub1, p1->sub2, p1->sub3, p1->ave);
flag--;
}
else
{
p1 = (struct student*)malloc(sizeof(struct student));
fscanf(fp, "%s %s %d %d %d %d\n", p1->num, p1->name, &p1->sub1, &p1->sub2, &p1->sub3, &p1->ave);
printf("%s\t%s\t%d\t%d\t%d\t%d\n", p1->num, p1->name, p1->sub1, p1->sub2, p1->sub3, p1->ave);
p2->next = p1;
p2 = p1;
}
}
p2->next = NULL;
fclose(fp);
return head;
}
int main()
{
creat();
save(head);
read();
}