0% found this document useful (0 votes)
45 views8 pages

Implement Sequential File Perform Follwing Opeartions: Display Add Records Search Record Modify Record Delete Record Solution

The document describes a C program that implements sequential file operations to perform functions like display, add, search, modify, and delete records of student data stored in a text file. The program uses structures to define student records with roll number, name, and marks as fields. It contains functions to insert, display, search, delete, and modify records in the file and a menu-driven switch case to call the appropriate function based on user input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views8 pages

Implement Sequential File Perform Follwing Opeartions: Display Add Records Search Record Modify Record Delete Record Solution

The document describes a C program that implements sequential file operations to perform functions like display, add, search, modify, and delete records of student data stored in a text file. The program uses structures to define student records with roll number, name, and marks as fields. It contains functions to insert, display, search, delete, and modify records in the file and a menu-driven switch case to call the appropriate function based on user input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 8

6.

Implement sequential file perform follwing opeartions:


1. Display
2. Add records
3. Search record
4. Modify record
5. Delete record

Solution:

#include <stdio.h>
#include <stdlib.h>
typedef struct Student
{
int rollno,marks;
char name[30];
}Student;
int i;

void insert();
void disp();
void search();
void del();
void modify();

FILE *fp1,*fp2;

int main()
{
int ch;
if(!(fp1=fopen("Student.txt","r+")))
fp1 = fopen("Student.txt","w+");
do
{
printf("\n\n 1.Insert \n 2.Delete \n 3.Modify \n 4.Search \n 5.Display \n 6.Exit
");
printf("\n Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
disp();
break;
case 2:
del();
disp();
break;
case 3:
modify();
disp();
break;
case 4:
search();
disp();
break;
case 5:
disp();
break;
default:
printf("\n Invalid Choice!!!!!");
}
}while(ch!=6);
return 0;
}

void insert()
{
Student st[50];
int i,n;
fseek(fp1,0,2);
printf("\n Enter the no of students: ");
scanf("%d",&n);
printf("\n Enter the Roll No, Name and Marks: ");
for(i=0;i<n;i++)
{
scanf("%d%s%d",&st[i].rollno,st[i].name,&st[i].marks);
fprintf(fp1,"\n %d %s %d ",st[i].rollno,st[i].name,st[i].marks);
}

fclose(fp1);
fp1=fopen("Student.txt","r+");
}

void disp()
{
Student st;
rewind(fp1);
printf("\n Data in the file is: ");
while(!feof(fp1))
{
fscanf(fp1,"%d%s%d",&st.rollno,st.name,&st.marks);
if (feof(fp1))
break;
printf("\n %d %s %d",st.rollno,st.name,st.marks);
}
}

void search()
{
Student st;
int roll_no,res=0;
printf("\n Enter roll no to be searched: ");
scanf("%d",&roll_no);
rewind(fp1);
while(!feof(fp1))
{
fscanf(fp1,"%d%s%d",&st.rollno,st.name,&st.marks);
if(st.rollno==roll_no)
{
printf("\n Record is found!!");
printf("%d\t%s\t%d\n",st.rollno,st.name,st.marks);
res=1;
break;
}
}
if (res==0)
{
printf("\n Record Not Found!!!");
}
}

void del()
{
Student st;
int recno,i;
printf("\n Enter the record no to be deleted : ");
scanf("%d",&recno);
fp2=fopen("temp.txt","w+");
rewind(fp1);
for(i=1;i<recno&&!feof(fp1);i++)
{
fscanf(fp1,"%d%s%d",&st.rollno,st.name,&st.marks);
fprintf(fp2,"\n %d %s %d",st.rollno,st.name,st.marks);
}
if(feof(fp1))
{
printf("Record out of range !!");
}
else
{
fscanf(fp1,"%d%s%d",&st.rollno,st.name,&st.marks);
while(!feof(fp1))
{
fscanf(fp1,"%d%s%d",&st.rollno,st.name,&st.marks);
fprintf(fp2,"\n %d %s %d",st.rollno,st.name,st.marks);
}
fclose(fp1);
fclose(fp2);
fp1=fopen("Student.txt","w+");
fp2=fopen("temp.txt","r+");
while(!feof(fp2))
{
fscanf(fp2,"%d%s%d",&st.rollno,st.name,&st.marks);
fprintf(fp1,"\n %d %s %d",st.rollno,st.name,st.marks);
}
printf("\n Record Deleted successfully!! ");
}
fclose(fp2);
}

void modify()
{
Student st;
int recno,i;
printf("\n Enter the record no to be modified : ");
scanf("%d",&recno);
fp2=fopen("temp.txt","w+");
rewind(fp1);
for(i=1;i<recno&&!feof(fp1);i++)
{
fscanf(fp1,"%d%s%d",&st.rollno,st.name,&st.marks);
fprintf(fp2,"\n %d %s %d",st.rollno,st.name,st.marks);
}
if(feof(fp1))
{
printf("Record out of range !!");
}
else
{
fscanf(fp1,"%d%s%d",&st.rollno,st.name,&st.marks);
printf("\n Enter rollno , Name and marks: ");
scanf("%d%s%d",&st.rollno,st.name,&st.marks);
fprintf(fp2,"\n %d %s %d",st.rollno,st.name,st.marks);
while(!feof(fp1))
{
fscanf(fp1,"%d%s%d",&st.rollno,st.name,&st.marks);
fprintf(fp2,"\n %d %s %d",st.rollno,st.name,st.marks);
}
fclose(fp1);
fclose(fp2);
fp1=fopen("Student.txt","w+");
fp2=fopen("temp.txt","r+");
while(!feof(fp2))
{
fscanf(fp2,"%d%s%d",&st.rollno,st.name,&st.marks);
fprintf(fp1,"\n %d %s %d",st.rollno,st.name,st.marks);
}
}
fclose(fp2);
}

Output:

/*
1.Insert
2.Delete
3.Modify
4.Search
5.Display
6.Exit
Enter your choice : 1

Enter the no of students: 3

Enter the Roll No, Name and Marks: 10


shubham
50
12
suchit
60
14
suyog
70

Data in the file is:


10 shubham 50
12 suchit 60
14 suyog 70

1.Insert
2.Delete
3.Modify
4.Search
5.Display
6.Exit
Enter your choice : 2

Enter the record no to be deleted : 3

Record Deleted successfully!!


Data in the file is:
10 shubham 50
12 suchit 60

1.Insert
2.Delete
3.Modify
4.Search
5.Display
6.Exit
Enter your choice : 3

Enter the record no to be modified : 1

Enter rollno , Name and marks: 10


shubham
80
Data in the file is:
10 shubham 80
12 suchit 60

1.Insert
2.Delete
3.Modify
4.Search
5.Display
6.Exit
Enter your choice : 4

Enter roll no to be searched: 10

Record is found!!10 shubham 80

Data in the file is:


10 shubham 80
12 suchit 60

1.Insert
2.Delete
3.Modify
4.Search
5.Display
6.Exit
Enter your choice : 5

Data in the file is:


10 shubham 80
12 suchit 60

1.Insert
2.Delete
3.Modify
4.Search
5.Display
6.Exit
Enter your choice : 6

Invalid Choice!!!!!

------------------
(program exited with code: 0)
Press return to continue

*/

You might also like