Implement Sequential File in C
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 10
struct stud
{
char name[20];
int rollno;
int per;
};
void addrecord()
{
struct stud temp;
FILE *fptr;
fptr = fopen("myFile.txt","ab");
if(fptr==NULL)
{
printf("file not opened");
return ;
}
fflush(stdin);
printf("\nEnter the Name :\n");
scanf("%s",&temp.name);
printf("Enter the Roll no :\n");
scanf("%d",&temp.rollno);
printf("Enter the Percentage:\n");
scanf("%d",&temp.per);
fwrite(&temp, sizeof(struct stud), 1, fptr);
fclose(fptr);
printf("\n \nRecord has been added.");
}
void disp()
{
struct stud temp;
FILE *fptr;
fptr=fopen("myFile.txt","rb");
if(fptr==NULL)
{
printf("file not opened");
return ;
}
printf("\nName\tRollno\tPecentage \n");
printf("\n==========================");
while(fread(&temp, sizeof(struct stud), 1, fptr)==1)
{
printf("\n%s\t%d\t%d", temp.name, temp.rollno,temp.per);
}
fclose(fptr);
}
void search(char *n)
{
struct stud temp;
FILE *fptr;
fptr=fopen("myFile.txt","rb");
if(fptr==NULL)
{
printf("file not opened");
return;
}
int j=0;
while(fread(&temp, sizeof(struct stud), 1, fptr)==1)
{
if(strcmp(n,temp.name)==0)
{
j=1;
printf("\nRecord found!!!");
return;
}
}
if(j==0)
printf("\nRecord not found!!!");
}
void modify(char *n)
{
struct stud temp[SIZE];
FILE *fptr;
fptr=fopen("myFile.txt","rb");
if(fptr==NULL)
{
printf("file not opened");
return ;
}
int i=0,j,k=0;
while(fread(&temp[i], sizeof(struct stud), 1, fptr)==1)
{
++i;
}
fclose(fptr);
fptr = fopen("myFile.txt","wb");
if(fptr==NULL)
{
printf("file not opened");
return ;
}
for(j=0;j<i;j++)
{
if(strcmp(n,temp[j].name)==0)
{
k=1;
printf("\nEnter the new marks :\n");
scanf("%d",&temp[j].per);
printf("\nRecord modified");
}
fwrite(&temp[j],sizeof(struct stud),1,fptr);
}
fclose(fptr);
if(k==0)
{
printf("\nRecord not found.!! :(\n");
}
}
void delete(char *n)
{
struct stud temp[SIZE];
FILE *fptr;
fptr=fopen("myFile.txt","rb");
if(fptr==NULL)
{
printf("file not opened");
return;
}
int i=0,j,k=0;
while(fread(&temp[i], sizeof(struct stud), 1, fptr)==1)
{
i++;
}
fclose(fptr);
fptr = fopen("myFile.txt","wb");
if(fptr==NULL)
{
printf("file not opened");
return ;
}
for(j=0;j<i;j++)
{
if(strcmp(n,temp[j].name)==0)
{k=1;
printf("\nRecord found!!! :)");
printf("\n Record deleted \n");
continue;
}
fwrite(&temp[j],sizeof(struct stud),1,fptr);
}
fclose(fptr);
if(k==0)
{
printf("\n record not found");
return;
}
}
int main()
{
int c,g;
char t[20];
setbuf(stdout,NULL);
do
{
printf("\n-------MENU-------");
printf("\n1.ADD RECORD");
printf("\n2.DISPLAY");
printf("\n3.SEARCH");
printf("\n4.MODIFY");
printf("\n5.DELETE");
printf("\n6.EXIT\n");
printf("Please enter your choice ");
scanf("%d",&c);
switch(c)
{
case 1:
addrecord();
break;
case 2:
disp();
break;
case 3:
printf("\nEnter the name to search :\n");
scanf("%s",t);
search(t);
break;
case 4:
printf("\nEnter the name to modify :\n");
scanf("%s",t);
modify(t);
break;
case 5:
printf("\nEnter the name to delete :\n");
scanf("%s",t);
delete(t);
break;
case 6: exit(0);
}
printf("\nDo you want to continue?Enter 1 for Yes or enter 0 for No : ");
scanf("%d",&g);
}while(g==1);
return 0;
}
Output:
-------MENU-------
1.ADD RECORD
2.DISPLAY
3.SEARCH
4.MODIFY
5.DELETE
6.EXIT
Please enter your choice 1
Enter the Name :
aa
Enter the Roll no :
11
Enter the Percentage:
11
Record has been added.
Do you want to continue?Enter 1 for Yes or enter 0 for No : 1
-------MENU-------
1.ADD RECORD
2.DISPLAY
3.SEARCH
4.MODIFY
5.DELETE
6.EXIT
Please enter your choice 2
Name Rollno Pecentage
==========================
aa 11 11
Do you want to continue?Enter 1 for Yes or enter 0 for No : 1
-------MENU-------
1.ADD RECORD
2.DISPLAY
3.SEARCH
4.MODIFY
5.DELETE
6.EXIT
Please enter your choice 3
Enter the name to search :
bb
Record not found!!!
Do you want to continue?Enter 1 for Yes or enter 0 for No : 1
-------MENU-------
1.ADD RECORD
2.DISPLAY
3.SEARCH
4.MODIFY
5.DELETE
6.EXIT
Please enter your choice 4
Enter the name to modify :
aa
Enter the new marks :
12
Record modified
Do you want to continue?Enter 1 for Yes or enter 0 for No : 1
-------MENU-------
1.ADD RECORD
2.DISPLAY
3.SEARCH
4.MODIFY
5.DELETE
6.EXIT
Please enter your choice 2
Name Rollno Pecentage
==========================
aa 11 12
Do you want to continue?Enter 1 for Yes or enter 0 for No : 1
-------MENU-------
1.ADD RECORD
2.DISPLAY
3.SEARCH
4.MODIFY
5.DELETE
6.EXIT
Please enter your choice 5
Enter the name to delete :
aa
Record found!!! :)
Record deleted
Do you want to continue?Enter 1 for Yes or enter 0 for No : 1
-------MENU-------
1.ADD RECORD
2.DISPLAY
3.SEARCH
4.MODIFY
5.DELETE
6.EXIT
Please enter your choice 2
Name Rollno Pecentage
==========================
Do you want to continue?Enter 1 for Yes or enter 0 for No :
No comments:
Post a Comment