输入数据是分开几次输入,但输出是把几次输入的数据一起输出。
本人做了,但现实不到题所说的。高手请指点。
#include <stdio.h>
#include <string.h>
struct tele /*defines the tele*/
{
char name[20];
long int num;
char address[30];
}abc; /*declares a variable*/
main()
{ int choice=1;
{
printf("1 add new subscriber details\n");
printf("2 view directory\n");
printf("3 quit\n");
printf("\n\n Enter choice:");
scanf ("%d",&choice);
if (choice==1)
addsubs();
else if(choice==2)
viewdir();
else if(choice==3)
exit();
}
}
addsubs()
{
FILE *fp;
struct tele abc;
int n;
char flage='y';
if ((fp=fopen("telephone","a"))==NULL)
{printf("\nerror open tele file");
}
while(flage=='y')
{printf(" enter cust name:");
scanf ("%s",&abc.name);
printf(" Enter cust No.:");
scanf("%d",&n);
if (2000000>n||n>2999999)
{printf ("sub tele no. must be between 2000000 and 2999999");
printf(" Enter cust No.:");
scanf("%d",&n);
}
abc.num=n;
printf(" Enter address:");
scanf("%s",&abc.address);
getchar ();
fwrite(&abc,sizeof(struct tele),1,fp);
printf("\n\n continue?(Y/N)");
scanf("%c",&flage);
}
fclose(fp);
}
viewdir()
{ FILE *fp;
char flage='y';
int i;
struct tele abc;
if((fp=fopen("telephone","r"))==NULL)
{ printf("打开文件出错\n");}
for (i=0;i<3;i++)
{fread(&abc,sizeof(struct tele),1,fp);
printf(" The customer name is %s\n",abc.name);
printf(" The customer no is %d\n",abc.num);
printf(" The customer address is %s\n",abc.address);
}
fclose(fp);
}