我运行了,编译可以通过。文件读入和输出、删除、查找都可以!但是,插入(insert函数)无法实现!
我用的文件和链表!请大家帮忙!
#include<stdio.h>
#include<math.h>
#include<malloc.h>
#include<stddef.h>
#include<string.h>
#define LEN sizeof(struct stud)/*宏定义*/
struct stud
{char name[10];
int num;
int scores;
struct stud *next;
};
struct stud*p1,*p2,*head;
FILE *fp;
int n;
void creat()
{if((fp=fopen("s_scores.txt","w"))==NULL)
{printf("Can't open file!\n");
exit(0);
}
for(p1=head;p1!=NULL;p1=p1->next)
if(fwrite(p1,LEN,1,fp)==NULL)
printf("File write error!\n");
fclose(fp);
}
struct stud *input()/*链式输入,并保存到磁盘*/
{char ch;
int i=1;
p1=(struct stud *)malloc(LEN);
head=p1;
while(i)
{printf("input the name:\n");
scanf("%s",p1->name);
printf("input the number:\n");
scanf("%d",&p1->num);
printf("input the scores:\n");
scanf("%d",&p1->scores);
getchar();
printf("another?y/n");
ch=getchar();
if(ch=='n'||ch=='N')
{ i=0;
p1->next=NULL;
}
else
{p2=p1;
p1=(struct stud *)malloc(LEN);
p2->next=p1;
}
}
creat();/*存盘*/
return(head);}
struct stud *output(struct stud *head)/*输出函数*/
{ if((fp=fopen("s_scores.txt","r"))==NULL)/*打开文件*/
{printf("Can't read the file!\n");
exit(0);}
for(p1=head;p1!=NULL;p1=p1->next)/*读文件*/
{fread(p1,LEN,1,fp);
printf("%-10s%-6d%-4d\n",p1->name,p1->num,p1->scores);
}getchar();
printf("any key continue...\n");
getchar();/*便于在屏幕上显示,可以看到输出结果*/
return(p1);}
void search(struct stud *head)/*查找函数*/
{char name[10];
p1=(struct stud *)malloc(LEN);/*分配一个内存单元*/
printf("Please put the name which you want to find:\n");
scanf("%s",name);/*从键盘输入名字*/
if((fp=fopen("s_scores.txt","r"))==NULL)/*以读的形式打开*/
printf("Unluckily!File open failed!");
for(p1=head;p1!=NULL;p1=p1->next)
{fread(p1,LEN,1,fp);
if(strcmp(name,p1->name)==0)/*和名字比较,相同就输出*/
{printf("%s\n",p1->name);
printf("%d\n",p1->num);
printf("%d\n",p1->scores);break;}}
printf("any key continue!\n");
getchar();
getchar();
fclose(fp);}
struct stud *del(struct stud *head)/*删除一组数据*/
{int num;
p1=(struct stud *)malloc(LEN);
printf("please put the num which you want to del!\n");
scanf("%d",&num);
if((fp=fopen("s_scores","w"))==NULL)/*以写的形式打开*/
printf("Unlucily!file write error!");
{{ p1=head;
while(num!=p1->num&&p1->next!=NULL)
{ p2=p1;
p1=p1->next;
}
if(num==p1->num)
{if(p1==head)
head=p1->next;
else p2->next=p1->next;
for(p1=head;p1!=NULL;p1=p1->next)
fwrite(p1,LEN,1,fp);/*保存到磁盘*/
fclose(fp); }}
getchar();
printf("any key continue.......\n");
getchar(); }
return(head);
}
void insert(struct stud *p2)/*插入一组数据,插入到最后*/
{struct stud *p3;
p3=(struct stud *)malloc(LEN);
printf("put the name:\n");
scanf("%s",p3->name);
printf("put the num:\n");
scanf("%d",&p3->num);
printf("put the scores:\n");
scanf("%d",&p3->scores);
p2=p3;
p2->next=NULL;/*把尾地址赋给该节点*/
if((fp=fopen("s_scores","w"))==NULL)
printf("file open error!\n");
{ for(p1=head;p1!=NULL;p1=p1->next)
fwrite(p1,LEN,1,fp);
fclose(fp);}
getchar();
printf("any key continue....\n");
getchar();}
main()
{char i;
while(1)/*循环执行,当i='6'的时候跳出*/
{clrscr();
printf("~~~~~~~~~~~~~~wellcome~~~~~~~~~~~~~~\n");
printf("1)input 2)output 3)search\n");
printf("4)del 5)insert 6)exit\n");
printf("~~~~~~~~~~~~~~~system~~~~~~~~~~~~~~~\n");
printf("***********HAPPY NEW YEAR**************\n");
printf("please put your choice:\n");
i=getchar();
switch(i)
{case '1':head=input();break;
case '2':p2=output(head);break;
case '3':search(head);break;
case '4':head=del(head);break;
case '5':insert(p2);break;
case '6':exit(0);break;
}
}
}
[此贴子已经被作者于2006-1-7 14:31:27编辑过]