Sunday 22 July 2012

Add and display record using structures and file

How to add and display record using structures and file (fprintf) 

/*Program to add and display record using structures and file*/

#include<stdio.h>
#include<conio.h>

//structure for person
struct person
{
char name[30];
unsigned long pn;
};

//main function
int main()
{
int choice;
FILE *fp;
struct person p;
clrscr();

//open file in append mode
fp=fopen("person.txt","a+");
if(fp==NULL)
{
printf("FILE CAN'T OPEN.\n");
exit(1);
}

//menu
while(1)
{
printf("\n1.Add Record\n");
printf("2.Display Records\n");
printf("3.EXIT\n");
printf("Enter your choice...\n");
scanf("%d",&choice);


switch(choice)
{

//Add Record in file
case 1:printf("Enter name & Phone number\n");
scanf("\n\n %s %lu",p.name,&p.pn);
fprintf(fp,"%s %pn \n" ,p.name,p.pn);
break;

//Display records from begining
case 2:fseek(fp,0,SEEK_SET);
while(fscanf(fp,"%s %lu",p.name,&p.pn)!=EOF)
printf("\n\n %s %lu \n",p.name,p.pn);
break;

//exit
case 3:exit(1);
default:printf("UNKNOWN CHOICE\n");
}
}

//close file
fclose(fp);
}

 

Share This
Previous Post
Next Post

FYJC XI standard online admisson Process and declaraton of Merit list . Cut off List For prevous year also . 10 Th Results onlne declaraton Maharashtra Region .

0 comments: