RUPAK SEARCH ENGINE

Custom Search

Write a program to create/append/disp data of a data file.

/*Write a program to create/append/disp data of a data file.
Also use structure.*/
#include
#include
#include
#include
# define null 0
struct info{
int roll;
char name[20];
}student;
FILE *ptr,*ptr1;
void dispmenu();
void create(void);
void add(void);
void dispdata(void);
void disp_one(void);
void modify(void);
void del_one(void);
void del_all(void);
void main()
{
int choice;
repeat:
dispmenu();
scanf("%d",&choice);
if(choice<1|| choice>8)
goto repeat;
else if(choice==1)
create();
else if(choice==2)
add();
else if(choice==3)
dispdata();
else if(choice==4)
disp_one();
else if(choice==5)
modify();
else if(choice==6)
del_one();
else if(choice==7)
del_all();
else if(choice==8)
exit(1);
goto repeat;
}
void create()
{
ptr=fopen("myfile.dat","w");
clrscr();
printf("Roll:");
scanf("%d",&student.roll);
printf("name");
scanf("%s",student.name);
fprintf(ptr,"%d%s\n",student.roll,student.name);
fclose(ptr);
}
void add()
{
clrscr();
ptr=fopen("myfile.dat","a");
printf("Roll:");
scanf("%d",&student.roll);
printf("name:");
scanf("%s",student.name);
fprintf(ptr,"%d%s\n",student.roll,student.name);
fclose(ptr);
}
void dispdata()
{
clrscr();
if((ptr=fopen("myfile.dat","r"))==null)
printf("File not found.");
else
{
while(1)
{
fscanf(ptr,"%d",&student.roll);
fscanf(ptr,"%[^\n]",student.name);
if(student.roll==null)
break;
printf("%d ",student.roll);
printf("%s\n",student.name);
student.roll=null;
}
getch();
fclose(ptr);
}
}
void disp_one()
{
int roll;
clrscr();
if((ptr=fopen("myfile.dat","r"))==null)
printf("File not found.");
else
{
printf("Enter roll:");
scanf("%d",&roll);
do
{
fscanf(ptr,"%d",&student.roll);
fscanf(ptr,"%[^\n]",student.name);
if(roll==student.roll)
{
printf("%d ",student.roll);
printf("%s\n",student.name);
break;
}
}while(feof(ptr)==0);
getch();
fclose(ptr);
}
}
void modify()
{
int roll;char name[20];
clrscr();
if((ptr=fopen("myfile.dat","r"))==null)
printf("File not found.");
else
{
printf("Enter roll:");
scanf("%d",&roll);
printf("Enter name:");
scanf("%s",name);
ptr1=fopen("test","w");
do
{
fscanf(ptr,"%d",&student.roll);
fscanf(ptr,"%[^\n]",student.name);
if(roll==student.roll)
{
student.roll=roll;
strcpy(student.name,name);
}
fprintf(ptr1,"%d%s\n",student.roll,student.name);
}while(feof(ptr)==0);
getch();
fclose(ptr);
fclose(ptr1);
remove("myfile.dat");
rename("test","myfile.dat");
}
}
void del_one()
{
int roll;char name[20];
clrscr();
if((ptr=fopen("myfile.dat","r"))==null)
printf("File not found.");
else
{
printf("Enter roll:");
scanf("%d",&roll);
ptr1=fopen("test","w");
while(1)
{
fscanf(ptr,"%d",&student.roll);
fscanf(ptr,"%[^\n]",student.name);
if(student.roll==null)
break;
else if(student.roll==roll)
;
else
{
fprintf(ptr1,"%d%s\n",student.roll,student.name);
student.roll=0;
}
}
fclose(ptr);
fclose(ptr1);
remove("myfile.dat");
rename("test","myfile.dat");
}
}

void del_all()
{
remove("myfile.dat");
}
void dispmenu()
{
clrscr();
printf("\n1.Create New File.\n2.Add more(append)");
printf("\n3.Display all\n4.Disp particular");
printf("\n5.Modify one\n6.Delete one");
printf("\n7.Delete all\n8.Exit\nChoose 1 to 8 only");
}

No comments:

Post a Comment