标签:
对通讯录的操作都已经实现了,但前提是已经有一个通讯录了。那么第一次进入的时候我们需要建立一个通讯录,让前面的主程序读取。于是就有了这个“安装程序”,第一次运行通讯录前,让用户运行这个程序,将 数据,file_name,len 存入相应文件。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stu
{
int num;
char name[20],tel[13],email[30];
char sort;
char yn;
struct stu *next;
};
main()
{
FILE *in,*f_name,*ll;
char fname[50],ch;
struct stu stud[15],ss[15];
int i,len=0;
loop:printf("\nplease enter the number of contacts :");
scanf("%d",&len);
if(len>15)
{
printf("\n* The number should be less than 15\n");
goto loop;
}
puts("\n\n* Tips: when you enter ‘sort‘ information, ‘a‘ means official, ‘b‘ means personal, ‘c‘ means commercial *\n");
printf("\n\n ******************* Enter information *******************\n\n");
for(i=0;i<len;i++)
{
printf("\t\tname: ");
scanf("%s",stud[i].name);
printf("\t\ttel : ");
scanf("%s",stud[i].tel);
printf("\t\tsort: ");
getchar();
stud[i].sort=getchar();
getchar();
printf("\n\t\temail: ");
scanf("%s",stud[i].email);
puts("\n");
}
if((in=fopen("D:\\telbook.dat","wb"))==NULL)
{
puts("cannot open the file");
exit (0);
}
for(i=0;i<len;i++)
fwrite(&stud[i],sizeof(struct stu),1,in);
fclose(in);
if((f_name=fopen("D:\\file_name.dat","wb"))==NULL)
{
puts("cannot open the file");
exit (0);
}
rewind(f_name);
fputs("D:\\telbook.dat",f_name); // note filename
fclose(f_name);
if((in=fopen("D:\\len.dat","wb"))==NULL)
{
puts("cannot open the file");
exit(0);
}
rewind(in);
fputc(len,in);
fclose(in);
//check
puts("\n\nCheck");
if((in=fopen("D:\\telbook.dat","rb"))==NULL)
{
puts("cannot open the file");
exit (0);
}
for(i=0;i<len;i++)
fread(&ss[i],sizeof(struct stu),1,in);
fclose(in);
if((f_name=fopen("D:\\file_name.dat","rb"))==NULL)
{
puts("cannot open the file");
exit (0);
}
fgets(fname,49,f_name); // note filename
fclose(f_name);
printf("\n\nfile name : %s\n\n\n",fname);
for(i=0;i<len;i++)
{
printf("\t\tname: %s\n",ss[i].name);
printf("\t\ttel : %s\n",ss[i].tel);
printf("\t\tsort: %c",ss[i].sort);
printf("\n\t\temail: %s",ss[i].email);
puts("\n");
}
if((ll=fopen("D:\\len.dat","rb"))==NULL)
{
puts("cannot open the file len");
exit(0);
}
rewind(ll);
ch=fgetc(ll);
fclose(ll);
printf("\n\nsaved len : %d\n",ch);
//check end
}
标签:
原文地址:http://www.cnblogs.com/GY8023/p/4556040.html