2 ligaogang ligaogang 于 2013.10.27 00:03 提问

用链表做一个成绩管理系统,记录写在student.txt中,有些问题。望回答。

#include "stdio.h"
#include "io.h"
#include "malloc.h"
#define MAXSIZE 100
#define LEN 5
typedef char DataType;
typedef struct student{
DataType num[LEN],score[LEN];
DataType name[LEN];
}student;
typedef struct listnode{
student data;
int len;
struct listnode next;
}listnode;
//获得第n个节点
listnode
getnode(listnode q,int n)
{
int m=0;
listnode *s=q;
if(nq->len)
return NULL;
while(m!=n&&s->next)
{
s=s->next;
m++;
}
if(m==n)
return s;
else
return NULL;
}
//按位置插入,并写入文件
void insert(listnode *q,int n,FILE *fp)
{
listnode *s,*r;
int m;
s=(listnode
)malloc(sizeof(listnode));
printf("输入姓名\n");
fflush(stdin);
gets(s->data.name);
printf("输入学号\n");
fflush(stdin);
gets(s->data.num);
printf("输入分数\n");
fflush(stdin);
gets(s->data.score);
r=getnode(q,n-1);
s->next=r->next;
r->next=r;
fseek(fp,(n-1)*sizeof(struct student),0);
fprintf(fp,"\n%s\t\t%s\t\t%s\n",s->data.num,s->data.name,s->data.score);
}
//删除记录
void del(int n,FILE *fp)
{
listnode *head,*s,*r;
FILE *fp1;
head=ennc(fp);
s=getnode(head,n-1);
r=s->next;
s->next=r->next;
free(r);
fp1=enfile(head);
return fp1;

}
//调入内存
listnode* ennc(FILE fp)
{
char ch;
listnode *head,*s,*r;
head=(listnode
)malloc(sizeof(listnode));
head->next=NULL;
r=head;

ch=fgetc(fp);
if(ch==EOF)
{
printf("文件为空,按任意键退出\n");
getch();
exit(1);
}
fseek(fp,0,0);
while(ch!=EOF)
{
s=(listnode*)malloc(sizeof(listnode));
r->next=s;
r=s;
r->next=NULL;
fscanf(fp,"%s%s%s\n",s->data.num,s->data.name,s->data.score);
printf("当前调用的内容是:\n");
printf("%s\t\t%s\t\t%s\t\t\n",s->data.num,s->data.name,s->data.score);
ch=fgetc(fp);
}
return head;
}
//将内存中的内容写入文件
FILE* enfile(listnode *head)
{
listnode *s;
FILE *fp;
if((fp=fopen("student.txt","wt+"))==NULL)
{
printf("cannot open student.txt ,press any key exit\n");
getchar();
exit(1);
}

s=head->next;
while(s)
{
    fprintf(fp,"%s\t\t%s\t\t%s\n",s->data.num,s->data.name,s->data.score);
    s=s->next;
}
return fp;

}
void main()
{
int num,i;
char ch;
FILE fp;
student *pp,*qq;
listnode *head,*s,*r;
head=(listnode
)malloc(sizeof(listnode));
head->next=NULL;
r=head;
//检查文件是否存在,存在则打开,不存在则创建
if((_access("student.txt",0))!=-1)
{
if((fp=fopen("student.txt","at+"))==NULL)
{
printf("cannot open student.txt,press any key exit\n");
ch=getchar();
exit(1);
}
}
else
{
if((fp=fopen("student.txt","wt+"))==NULL)
{
printf("cannot open student.txt ,press any key exit\n");
ch=getchar();
exit(1);
}
}
ch=fgetc(fp);
if(ch!=EOF)
{
printf("文件不为空,现在将文件中内容调入内存\n");
while(ch!=EOF)
{
s=(listnode*)malloc(sizeof(listnode));
r->next=s;
r=s;
r->next=NULL;
fseek(fp,0,0);
fscanf(fp,"%s%s%s\n",s->data.num,s->data.name,s->data.score);
printf("%s\t\t%s\t\t%s\n",s->data.num,s->data.name,s->data.score);

ch=fgetc(fp);
}
}
//fputs("学号",fp); fputs("\t\t",fp);fputs("姓名",fp);fputs("\t\t",fp);fputs("成绩",fp);
//创建记录并写入记录到文件
else
{
printf("文件为空,现在录入记录,输入你想建立的学生信息记录条数\n");
scanf("%d",&num);
head->len=num;
for(i=0;i {
s=(listnode*)malloc(sizeof(listnode));
r->next=s;
r=s;
r->next=NULL;
printf("现在录入第%d个学生的信息,输入姓名\n",i+1);
fflush(stdin);
gets(s->data.name);
printf("输入学号\n");
fflush(stdin);
scanf("%s",&s->data.num);
printf("输入成绩\n");
fflush(stdin);
scanf("%s",&s->data.score);
fprintf(fp,"%s\t\t%s\t\t%s\n",s->data.num,s->data.name,s->data.score);
//kong
fseek(fp,0,2);
ch=fgetc(fp);
if(ch==EOF)
printf("kong\n");
else
printf("此位置以后为空\n");
/*
r=(listnode*)malloc(sizeof(listnode));
r->next=NULL;
qq=&(r->data);
fseek(fp,2*sizeof(struct student)+4,0);
fscanf(fp,"%s\t\t%s\t\t%s\n",qq->num,qq->name,qq->score);
printf("%s\t\t%s\t\t%s\n",qq->num,qq->name,qq->score);
/
//fwrite(pp,sizeof(struct student),1,fp);

/

fputs(" \n ",fp);
fputs(pp->num,fp);

fputs("\t\t",fp);
fputs(pp->name,fp);
fputs("\t\t",fp);
fputs(pp->score,fp);
fputs(" \n ",fp);
fseek(fp,0,2);
*/
}
}
printf("输入你要删除的数的序号,第几个\n");
scanf("%d",&num);
//del(fp,num);

//rewind(fp);

}
Csdn user default icon
上传中...
上传图片
插入图片