Vous êtes sur la page 1sur 5

/*NAME-Vivek Rai.

DIV- S.E I.T-2


BATCH- B
ROLL NO-31
ASSIGNMENT NO- 6 -FILE HANDLEING.
*/

#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);
}

//Definition of the Insert fucntion


void insert(char file[MAX])
{
char ch='Y';
FILE *fp;
struct student s;
fp= fopen(file,"ab");
if(fp==NULL)
{
printf("\n can't open the file");
exit(0);
}
do
{
printf("\n 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);
}

//Definition of the Search fucntion


void search(char file[MAX],int key)
{
char ch='Y';
FILE *fp;
struct student s;
fp= fopen(file,"rb");
while(fread(&s,sizeof(s),1,fp))
{
if(key==s.rn)
{
printf("\n the data you have searched");
printf("\nthe roll no %d",s.rn);
printf("\nthe name is %s",s.name);
printf("\nthe marks is %f",s.marks);
break;
}
else
printf("\nFILE Not found");
}
fclose(fp);
}

//Definition of the Display fucntion


void display(char file[MAX])
{
char ch='Y';
int i=1;
FILE *fp;
struct student s;
fp= fopen(file,"rb");
printf("\nRoll no\t\tName\t\tPercentage\n");
while(fread(&s,sizeof(s),1,fp))
{
printf("\n%d\t\t%s\t\t%f",s.rn,s.name,s.marks);
}
fclose(fp);
}

//Definition of the Delete fucntion


void Delete(char file[MAX],int del)
{

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");
}

//Definition of the Modify fucntion


void modi(char file[MAX],int mod)
{
char ch='Y';
FILE *fs,*ft;
struct student s;
fs= fopen(file,"rb");
ft= fopen("temp.dat","ab");
if(fs==NULL)
{
printf("\n Can't open the file");
exit(0);
}
if(ft==NULL)
{
printf("\n Can't open the file");
exit(0);
}
while(fread(&s,sizeof(s),1,fs))
{
if(s.rn!=mod)
{
fwrite(&s,sizeof(s),1,ft);
}
if(s.rn==mod)
{
printf("\n Enter the rollno,name & marks");
scanf("%d %s %f",&s.rn,s.name,&s.marks);
fwrite(&s,sizeof(s),1,ft);
break;
}
}
fclose(fs);
fclose(ft);
remove(file);
rename("temp.dat",file);
printf("Your data has modified");
}

/*

*/

Vous aimerez peut-être aussi