C PROGRAMMING

write a c program to implement all operations of files

File handling with menu driven program All operations?

 

 #include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>

// Function deceleration.

void write_to_file(char file_name[]);
void read_form_file();
void read_specific_record(char file_name[]);
void update_file(char file_name[]);
void delete_record();
void delete_file(char filename[]);
void merge_2_files(char source[], char target[]);

//creating a structure to hold data
struct data
{
char name[20];
int price;

}d;

void main()
{
char file_name[30];
int choice;
char sourcefile[20], targetfile[20];


do
{
printf("nEnter the option to handle a filenn");
printf("Enter 1 to write in a filen");
printf("Enter 2 to read form a filen");
printf("Enter 3 to read form a speciefied filen");
printf("Enter 4 to update a particular record in a filen");
printf("Enter 5 to delete a particular record form a filen");
printf("Enter 6 to delete a particular filen");
printf("Enter 7 to merge two filesn");
printf("Enter 8 to exitn");
scanf("%d",&choice);

switch(choice)
{
case 1:
printf("nnEnter the name of file. With extension like.nnname.txt (if you are working with simple file)nnname.dat (if working with records)nn");
scanf("%s",&file_name);
write_to_file(file_name);

break;

case 2:
printf("Enter name of file you want to Readn");
scanf("%s",&file_name);
read_from_file(file_name);
break;

case 3:
printf("Enter name of file you want to Read Specific Recordn");
scanf("%s",&file_name);
read_specific_record(file_name);
break;

case 4:
printf("Enter name of file you want to Update a particular Recordn");
scanf("%s",&file_name);
update_file(file_name);
break;

case 5:
printf("Enter name of file from which you want to Delete a particular Recordn");
scanf("%s",&file_name);
delete_record(file_name);
break;

case 6:
printf("Enter name of File you want to Deleten");
scanf("%s",&file_name);
delete_file(file_name);
break;

case 7:
printf("Enter name of Source File you want to mergen");

scanf("%s",&sourcefile);

printf("Enter name of Destination File you want to merge inton");
scanf("%s",&targetfile);

merge_2_files(sourcefile,targetfile);

break;

case 8:

exit(0);

break;

default:
printf("You have entered a worng choice!");
}


}while(choice);
getch();
}

//Function Definition.
void write_to_file(char filename[20])
{
FILE *writefile;
char text;
int conti=1; //conti for continue

writefile=fopen(filename,"wb");

//printf("Enter the text in filen");

while(conti==1)
{
printf("nEnter Name of product := ");
scanf("%s",&d.name);
printf("nEnter Price of product := ");
scanf("%d",&d.price);

printf("n*****************n");
fwrite(&d,sizeof(d),1,writefile);

printf("Do you want to continue press 1 else press 0nn");
scanf("%d",&conti);
};



fclose(writefile);


}

read_from_file(char filename[20]) //functin to display records from a file.
{

FILE *readfiledata;
char text;

readfiledata=fopen(filename,"rb");
if(readfiledata == NULL)
{
printf("File Does not existn");
exit(0);
}
while(fread(&d,sizeof(d),1,readfiledata)==1)
{
printf("n Name of the prduct := %s",d.name);
printf("n Price of the produce := %d",d.price);
printf("n********************n");
}

fclose(readfiledata);
}

void read_specific_record(char filename[20]) //function to display a specific record.
{
FILE *read_record;
char record;
char key[20];

read_record=fopen(filename,"rb");
if(read_record == NULL)
{
printf("File Does not existn");
exit(0);
}
printf("Enter the name of the record n");
scanf("%s",&key);

while(fread(&d,sizeof(d),1,read_record)==1)
{
if(strcmp(key,d.name)==0)
{
printf("n Name of the prduct := %s",d.name);
printf("n Price of the produce := %d",d.price);
printf("n********************n");
}
}


fclose(read_record);
}
void update_file(char filename[20]) // function to update records in a file
{

FILE *update_file;
char updated_name[20];
int updated_price;
char key[20];


update_file=fopen(filename,"r+b");
if(update_file == NULL)
{
printf("File Does not existn");
exit(0);
}
//here printing data present in file.
while(fread(&d,sizeof(d),1,update_file)==1)
{

printf("n Name of the prduct := %s",d.name);
printf("n Price of the produce := %d",d.price);
printf("n********************n");
}//upto here.

//now getting which field to update
printf("Enter the name of the record you want to update n");
scanf("%s",&key);
// now data updating with
printf("Enter the new price of product n");
scanf("%d",&updated_price);


if(strcmp(key,d.name)==0)
{
fseek(update_file,ftell(update_file)-sizeof(d),1);

d.price=updated_price;

fwrite(&d,sizeof(d),1,update_file);
}


fclose(update_file);
}
void del ete_record(char filename[20]) //delete a specific record
{
FILE *read_record, *temp;
char record;
char key[20];
int flag=0;
int s;

read_record=fopen(filename,"rb");
temp=fopen("temp.dat","wb");
if(read_record == NULL)
{
printf("File Does not existn");
exit(0);
}
printf("Enter the name of the record you want to deleten");
scanf("%s",&key);

while(fread(&d,sizeof(d),1,read_record)==1)
{
if(strcmp(key,d.name)!=0)
{
fwrite(&d,sizeof(d),1,temp);
flag=1;
}
}
if(flag==1)
//printf("record deleted succesfully");


remove(filename);
fclose(temp);
s=rename("temp.dat",filename);
if (s==0)
{
printf("file renamedn");
}else
{
printf("could no rename");
}



fclose(read_record);
}
void delete_file(char filename[20]) // delete a file
{
FILE *delete_file;

delete_file=fopen(filename,"rb");
if(delete_file == NULL)
{
printf("File Does not existn");
exit(0);
}
fclose(delete_file);
remove(filename);
printf("File deleted successfullyn");
getch();
}


void merge_2_files(char source[20],char target[20]) //function to merge two files
{
FILE *sf,*tf;
sf=fopen(source,"rb");
tf=fopen(target,"ab");
int flag=0;

if(sf == NULL)
{
printf("File Does not existn");
exit(0);
}
while(fread(&d,sizeof(d),1,sf)==1)
{
fwrite(&d,sizeof(d),1,tf);
flag=1;
}
if(flag==1)
printf("files merged succesfully");

fclose(sf);
fclose(tf);

}

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button