关于C语言通讯录保存用户输入数据的问题~30分敬上,好的话再加~懂的来,谢谢~

来源:百度知道 编辑:UC知道 时间:2024/06/06 05:49:17
我用C语言编的通讯录程序,怎样才能实现保存输入的数据的功能?就是我输入,退出之后下次进来还可以载入以前的数据~懂的人好好给弄弄,有时间的话顺便给我提一些合理化建议。O(∩_∩)O谢谢
#include<stdlib.h>
#include<string.h>
#define NEW (struct node*)malloc(sizeof(struct node))
struct node
{ char name[20],tel[9],class[10];
struct node *next;
};
struct node *create()
{ struct node *h;
struct node *p,*q;
char name[20],class[10];
h=q=NULL;
printf("name:");
gets(name);
while(strlen(name)!=0)
{ p=NEW;
if(p==NULL)
{printf("Allocation failure\n");
exit(0);
}
strcpy(p->name,name);
printf("tel:");
gets(p->tel);
printf("class:");
gets(class);
gets(p->class,class);
p->next=NULL;
if(h==NULL)
h=p;
else
q->next=p;
q=p;
printf("name:");
gets(

可以建立一个txt文件,通过程序打开它,将每次输入的记录,写到txt文件中。
如程序:
#include "stdio.h"
#include "stdlib.h"
int main()
{FILE *fp;
char ch;
if((fp=fopen("f:\\1.txt","w"))==NULL)
/*生成文件1.txt在F盘中*/
{printf("can't open this filel");
exit(1);}
while((ch=getchar())!='\n')
fputc(ch,fp);
fclose(fp);
return 0;
}

如果,下次打开时还要用到上次输入的信息,加入一个导入文件的函数就可以了!
如程序:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int main()
{FILE *fp;
char c[20];
if((fp=fopen("f:\\1.txt","r"))==NULL)
{printf("can't open this filel");
exit(1);}
while(fgets(c,20,fp)!=NULL)
{printf("%s",c);}
fclose(fp);
return 0;
}
(在运行过第一个程序后,在运行此程序。)

这只是一个简单的提示,你个以根据此提示,写出你需要的程序。如有不明白的地方,看