该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include
#include
#define filename "sss.dat"
typedef struct
{
int no;
char *name;
int math;
}info;
typedef struct
{
info *link;
int length;
int maxsize;
}head;
void init(head*);
void menu(head*);
void listInfo(head *head);
void add(head *head,info in);
void addinfo(head *head);
void main()
{
head head;
head.length=0;
head.maxsize=0;
init(&head);
menu(&head);
}
void init(head *in)
{
FILE *fp;
info val;
fp=fopen(filename,"rt");
if(fp==NULL)
{
printf("文件读取失败!");
fp=fopen(filename,"at");
if(fp==NULL)
{
printf("文件创建失败!\n");
}
fclose(fp);
}
fp=fopen(filename,"rt");
rewind(fp);
while(feof(fp)!= 0){
//val=(info)malloc(sizeof(info));
fread(&val,sizeof(info),1,fp);
if(&val)
add(in,val);
else{
printf("error");
}
}
fclose(fp);
}
void write(head *in)
{
FILE *fp;
int i;
info temp;
fp=fopen(filename,"at");
if(fp==NULL)
{
printf("文件创建失败!");
}
rewind(fp);
for(i=0;ilength);i++){
temp=in->link[i];
fwrite(&temp,sizeof(info),1,fp);
//printf("%d",i);
}
fclose(fp);
menu(in);
}
void add(head *head,info in)
{
if(head->length==head->maxsize){
head->link=(info *)malloc((head->maxsize+10)*sizeof(info));
head->maxsize+=10;
}
head->link[head->length]=in;
head->length++;
}
void menu(head *head)
{
int op;
printf("\t\t*********************************\n");
printf("\t\t* 学生信息管理系统\t\t*\n");
printf("\t\t*\t\t\t\t*\n");
printf("\t\t* 1.添加学生信息\t\t*\n");
printf("\t\t* 2.查看学生信息\t\t*\n");
printf("\t\t* 7.保存信息\t\t\t*\n");
printf("\t\t* 8.退出\t\t\t*\n");
printf("\t\t*********************************\n");
scanf("%d",&op);
getchar();
switch(op){
case 1:
{
addinfo(head);
break;
}
case 2:
{
listInfo(head);
break;
}
case 7:
{
write(head);
break;
}
case 8:
{
exit(0);
break;
}
default:
{
menu(head);
break;
}
}
}
void addinfo(head *head)
{char *name=(char *)malloc(sizeof(char));
info *in=(info *)malloc(sizeof(info));
printf("请输入学号:");
scanf("%d",&(in->no));
printf("请输入姓名:");
scanf("%s",name);
in->name=name;
printf("请输入分数:");
scanf("%d",&(in->math));
add(head,*in);
menu(head);
}
void listInfo(head *head)
{
int i=0;
for(;ilength;i++){
printf("\t%d\n",head->link[i].no);
}
menu(head);
}
为什么调用init方法后head的link还是空啊