主题:[讨论]一个关于用链表编写学生管理系统的程序的问题
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#define N 1000
FILE*stu_information;
struct student
{
int numble;char name[50] ;char sex[50];int age; struct student *next;
} stu[N];
struct student *head,*operation,*test;
void input_first();
void show();
int main ()
{
input_first();
show();
return 0;
}
void input_first ()
{
int num,i;
stu_information=fopen("stu_information.txt","w");
printf("input zhe numble you want :\n");
scanf("%d",&num);
fprintf(stu_information,"xue_hao name sex age\n");
if(num==0)
{
head=NULL;
printf("wrong!\n");return ;
}
for(i=0;i<num;i++)
{
test=(struct student*)malloc(sizeof(struct student));
if(i==0)
head=operation=test;
else
operation->next=test;
operation=test;
printf("input numble,name,sex,and age:\n");
scanf("%d%s%s%d",&test->numble,test->name,test->sex ,&test->age );
}
operation->next=NULL;
operation = head;
while(operation!=NULL)
{
fprintf(stu_information,"%3d%s%6s%6d\n",operation->numble,operation->name,operation->sex,operation->age);
operation=operation->next;
}
fclose(stu_information);
}
void show()
{
int num=1;
int i;
char kong_bai[20];
stu_information=fopen("stu_information.txt","r");
for(i=0;i<4;i++)
{
fscanf(stu_information,"%s",kong_bai);
}
while(!feof(stu_information))
{
test=(struct student*)malloc(sizeof(struct student));
if(num==1)
head=operation=test;
else
operation->next=test;
operation=test;
fscanf(stu_information,"%d%s%s%d",&operation->numble,operation->name,operation->sex,&operation->age);
num++;
}
// printf("%d\n",num);
operation->next=NULL;
operation=head;
// test=test->next;
// operation=test;
while(operation!=NULL)
{
printf("%d %s %s %d\n",operation->numble,operation->name,operation->sex,operation->age);
operation=operation->next;
}
fclose(stu_information);
}
这里只是部分功能,但是在show函数里出现了问题就是他会多循环了一次,就是如果文件里有两组结构体的数据,但是show中会读取三组导致最后一次读取的内容是空的,为什么胡出现多读取一次呢就是多循环一次,及求解啊
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#define N 1000
FILE*stu_information;
struct student
{
int numble;char name[50] ;char sex[50];int age; struct student *next;
} stu[N];
struct student *head,*operation,*test;
void input_first();
void show();
int main ()
{
input_first();
show();
return 0;
}
void input_first ()
{
int num,i;
stu_information=fopen("stu_information.txt","w");
printf("input zhe numble you want :\n");
scanf("%d",&num);
fprintf(stu_information,"xue_hao name sex age\n");
if(num==0)
{
head=NULL;
printf("wrong!\n");return ;
}
for(i=0;i<num;i++)
{
test=(struct student*)malloc(sizeof(struct student));
if(i==0)
head=operation=test;
else
operation->next=test;
operation=test;
printf("input numble,name,sex,and age:\n");
scanf("%d%s%s%d",&test->numble,test->name,test->sex ,&test->age );
}
operation->next=NULL;
operation = head;
while(operation!=NULL)
{
fprintf(stu_information,"%3d%s%6s%6d\n",operation->numble,operation->name,operation->sex,operation->age);
operation=operation->next;
}
fclose(stu_information);
}
void show()
{
int num=1;
int i;
char kong_bai[20];
stu_information=fopen("stu_information.txt","r");
for(i=0;i<4;i++)
{
fscanf(stu_information,"%s",kong_bai);
}
while(!feof(stu_information))
{
test=(struct student*)malloc(sizeof(struct student));
if(num==1)
head=operation=test;
else
operation->next=test;
operation=test;
fscanf(stu_information,"%d%s%s%d",&operation->numble,operation->name,operation->sex,&operation->age);
num++;
}
// printf("%d\n",num);
operation->next=NULL;
operation=head;
// test=test->next;
// operation=test;
while(operation!=NULL)
{
printf("%d %s %s %d\n",operation->numble,operation->name,operation->sex,operation->age);
operation=operation->next;
}
fclose(stu_information);
}
这里只是部分功能,但是在show函数里出现了问题就是他会多循环了一次,就是如果文件里有两组结构体的数据,但是show中会读取三组导致最后一次读取的内容是空的,为什么胡出现多读取一次呢就是多循环一次,及求解啊