Write a program that will generate a data file containing a list of customers and their phone numbers. Use structure variables to store the names, city and phone numbers of each customer.
source file name: FileStruct.C
#include<stdio.h>
struct customer
{
char name[20];
char city[10];
long phone;
} ;
int main()
{
int i;
struct customer cust[5];
char ch;
FILE *fp;
//clrscr();
fp=fopen("cust.txt","w");
for(i=0;i<5;i++)
{
printf("\n enter customer information: \n");
scanf( "%s%s%ld",cust[i].name,cust[i].city,&cust[i].phone);
fprintf(fp, " %s %s %ld\n",cust[i].name,cust[i].city,cust[i].phone);
}
fclose(fp);
printf("\n reads data from same file & display on screen :\n");
fp=fopen("cust.txt","r");
while((fscanf(fp, "%s%s%ld",cust[i].name,cust[i].city,&cust[i].phone))!=EOF)
{
printf( " %s %s %ld\n",cust[i].name,cust[i].city,cust[i].phone);
}
fclose(fp);
//getch();
return 0;
}
output:
0 Comments
Post a Comment