Académique Documents
Professionnel Documents
Culture Documents
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 100
//Declaration of the structure
struct student
{
int rn;
char name[MAX];
float marks;
};
//Declareation of the functions
void create(char file[MAX]);
void insert(char file[MAX]);
void search(char file[MAX],int key);
void display(char file[MAX]);
void Delete(char file[MAX],int del);
void modi(char file[MAX],int mod);
int main()
{
int ch,ch1;
int key,del,mod;
char filename[MAX];
printf("\nEnter the file name");
scanf("%s",filename);
while(1)
{
printf("\n1.creating a file");
printf("\n2.Insert a file");
printf("\n3.Search record a file");
printf("\n4.Display record");
printf("\n5.Delete arecord");
printf("\n6.Modify record");
printf("\n7.Exit");
printf("\nEnter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1://calling of the create functiobn
create(filename);
break;
case 2://calling of the Insert functiobn
insert(filename);
break;
case 3://calling of the Search functiobn
printf("Enter the roll no. you want search");
scanf("%d",&key);
search(filename,key);
break;
case 4://calling of the Display functiobn
display(filename);
break;
case 5://calling of the Delete functiobn
printf("\nEnter the roll no. which you want delete");
scanf("%d",&del);
Delete(filename,del);
break;
case 6://calling of the create functiobn
printf("\nEnter the roll no. which you want modify");
scanf("%d",&mod);
modi(filename,mod);
break;
case 7:
exit(0);
default:
printf("Enter the correct value\n");
}
}
}
//Definition of the create fucntion
void create(char file1[MAX])
{
char ch='Y';
FILE *fp;
struct student s;
fp= fopen(file1,"wb");
if(fp==NULL)
{
printf("\n can't open the file");
exit(0);
}
do
{
printf("Enter the rollno,name & marks");
scanf("%d %s %f",&s.rn,s.name,&s.marks);
fwrite(&s,sizeof(s),1,fp);
printf("\n Enter y or Y to continue");
scanf("%c ",&ch);
scanf("%c",&ch);
}while((ch=='Y') || (ch=='y'));
fclose(fp);
}
char ch='Y';
FILE *fs,*ft;
struct student s;
fs= fopen(file,"rb");
ft= fopen("temp.dat","wb");
if(fs==NULL)
{
printf("\nCan't open the file");
exit(0);
}
if(ft==NULL)
{
printf("\nCan't open the file");
exit(0);
}
while(fread(&s,sizeof(s),1,fs))
{
if(s.rn!=del)
{
fwrite(&s,sizeof(s),1,ft);
}
}
fclose(fs);
fclose(ft);
remove(file);
rename("temp.dat",file);
printf("Your data has deleted");
}
/*
*/