This is a full  file I/O  solution where user enters some information on the terminal and this user input is then stored into the file. Also this is a structured file operation program which supports :

1) Insertion of a record
2) Deletion of a record
3) Updation of a record
4) Searching of a record

Note: This program assumes that user enters the data in the correct specified format and order as required for further processing. i.e. Where the input is number and user enters a character , the program will go into infinite loop. The input from the user is not validated in this program as the intent for writing this program is to understand file input output.

#include <stdio.h>
#define REC_SIZE 56
enum { NOTFOUND = 0, FOUND};
FILE *fp;
typedef struct record
{
int id;
char name[20];
char desig[30];
} Record;

void die(char* msg)
{
perror(msg);
exit(0);
}

void insert(num)
{
Record rec;
int i ;
fp = fopen(“Record.dat”, “a”);
if(!fp)
die(“fopen”);
for(i = 0 ; i< num ; i++)
{
printf(“\nEnter id (number): “);
scanf(“%d”,&rec.id); //User is expected to enter an integer here
printf(“\nEnter name : “);
getchar();
scanf(“%[^\n]”,rec.name);
printf(“\nEnter desig : “);
getchar();
scanf(“%[^\n]”,rec.desig);
fwrite(&rec,sizeof(struct record),1,fp);
}
fclose(fp);
}

FILE* search(int id)
{
FILE *fp;
short flag = NOTFOUND;
fp = fopen(“Record.dat”, “r”);
int temp;
char ch;
while( fread(&temp,sizeof(int),1,fp) != 0)
{
//printf(“temp: %d\n”,temp);
if(temp == id)
{
flag = FOUND;
break;
}
else
fseek(fp,sizeof(struct record)- sizeof(int),SEEK_CUR);
}
if(flag == NOTFOUND)
{
fp = NULL;
//printf(“Record Not Found!!!\n”);
}
else
{
fseek(fp,-sizeof(int),SEEK_CUR);
}
return fp;
}

void replace(char *initial , char * final)
{
strcpy(initial, final);
}

void delete(int id)
{
FILE *fp1 , *fp2 ,*fpfinal;
char ch;
int i = 0;
int prior = 0 ,tempfp;
if((fp1 = search(id)) == NULL)
{
printf(“Record Not Found”);
return;
}
fp2 = fopen(“temp.txt”,”w+”);
if(!fp2)
die(“fopen()”);
tempfp = ftell(fp1);
fseek(fp1,0,SEEK_SET);
prior = ftell(fp1);
while(prior < tempfp)
{
fputc(fgetc(fp1),fp2);
prior++;
}
fseek(fp1,sizeof(struct record),SEEK_CUR);
while((ch = fgetc(fp1)) != EOF)
{
fputc(ch,fp2);
}
fseek(fp2,0,SEEK_SET);
fpfinal = fopen(“Record.dat”,”w”);
while((ch = fgetc(fp2)) != EOF)
{
fputc(ch,fpfinal);
}
fclose(fp1);
fclose(fp2);
fclose(fpfinal);
if(remove(“temp.txt”) == -1)
die(“Error removing temp file”);
}

void update(int id)
{
int choice;
FILE *fp ,*fpnew;
Record temprecs;
if((fp = search(id)) == NULL)
{
printf(“Record not present!!\n”);
return;
}

Files IO
printf(“\nWhich field u wanna update ? \n1. Id 2. Name 3. Designation\n Enter : “);
scanf(“%d”,&choice); //User is expected to enter an integer here
fpnew = fopen(“Record.dat”,”r+”);
fseek(fpnew, ftell(fp), SEEK_SET);
fclose(fp);
switch (choice)
{
case 1 :
printf(“\nEnter new id : “);
scanf(“%d”,&temprecs.id); //User is expected to enter an integer here
fwrite(&temprecs.id,sizeof(temprecs.id),1,fpnew);
break;
case 2 :
printf(“\nEnter new name : “);
getchar();
scanf(“%[^\n]”,temprecs.name);
fseek(fpnew,sizeof(temprecs.id),SEEK_CUR);
fwrite(temprecs.name,sizeof(temprecs.name),1,fpnew);
break;
case 3 :
printf(“\nEnter new desig : “);
getchar();
scanf(“%[^\n]”,temprecs.desig);
fseek(fpnew,sizeof(temprecs.id)+ sizeof(temprecs.name),SEEK_CUR);
fwrite(temprecs.desig,sizeof(temprecs.desig),1,fpnew);
break;
default :
printf(“Wrong Choice\n”);
break;
}
fclose(fpnew);
}

void list_rec()
{
FILE *fp;
char line[REC_SIZE];
Record rec;
int nbytes,i=0;
fp = fopen(“Record.dat”,”r”);
if(fp == NULL)
die(“Open Record.dat”);
while(1)
{
nbytes = fread(&rec,sizeof(struct record),1,fp);
if(nbytes == 0)
break;
printf(“%d\t%s\t%s\n”,rec.id,rec.name,rec.desig);
}
fclose(fp);
}

int main()
{
int id, num, choice ;
while(1)
{
printf(“\n 1) Insert\n 2) Search\n 3) Delete\n 4) List\n 5) Update\n 6) Exit\n Enter choice : “);
scanf(“%d”,&choice); //User is expected to enter an integer here
switch (choice)
{
case 1 :
printf(“\nEnter num of records: “);
scanf(“%d”,&num);//User is expected to enter an integer here
insert(num);
break;
case 2 :
printf(“\nEnter id to search: “);
scanf(“%d”,&id); //User is expected to enter an integer here
if(search(id) == NULL)
{
printf(“\nRecord Not Found!!\n”);
}
else
printf(“\nRecord is present…!!!\n”);
break;
case 3 :
printf(“\nEnter id to delete: “);
scanf(“%d”,&id); //User is expected to enter an integer here
delete(id);
break;
case 4 :
list_rec();
break;
case 5 :
printf(“\nEnter id to update: “);
scanf(“%d”,&id); //User is expected to enter an integer here
update(id);
break;
case 6 :
exit(0);
default :
printf(“Wrong Choice\n”);
break;
}
}
}
Output in file “Record.dat” , in the same directory as the program resides.

For more reading about technology news in singapore and seo to online marketing do view more about other pages.

Written by Sourabh Bhunje

Sourabh Bhunje, B.E. IT from Pune University. Currently Working at Techliebe. Professional Skills: Programming - Software & Mobile, Web & Graphic Design, Localization, Content Writing, Sub-Titling etc. http://techliebe.com/about-us

Leave a Comment