Code Description
In this program we will learn how to add a new record, modify an existing record, delete an existing record and display all record(s) on a file using “C”. “C” is a very powerful language and considered to be the mother of all programming languages since it creates the base of a programmer.
In the below program we have used several functions, lets understand each operation in detail:
fopen():Â This function is used for opening a file.
Syntax:FILE pointer_name=fopen (“file_name”,”Mode”);
Example: fp=fopen(“e:/c/kaustav.txt”,”rb+”)
The various kinds of “Opening Modes” in Standard I/O are listed in below table
File Mode | Meaning of Mode | During In existence of file |
---|---|---|
r | Open for reading. | If the file does not exist, fopen() returns NULL. |
rb | Open for reading in binary mode. | If the file does not exist, fopen() returns NULL. |
w | Open for writing. | If the file exists, its contents are overwritten. If the file does not exist, it will be created. |
wb | Open for writing in binary mode. | If the file exists, its contents are overwritten. If the file does not exist, it will be created. |
a | Open for append. i.e, Data is added to end of file. | If the file does not exists, it will be created. |
ab | Open for append in binary mode. i.e, Data is added to end of file. | If the file does not exists, it will be created. |
r+ | Open for both reading and writing. | If the file does not exist, fopen() returns NULL. |
rb+ | Open for both reading and writing in binary mode. | If the file does not exist, fopen() returns NULL. |
w+ | Open for both reading and writing. | If the file exists, its contents are overwritten. If the file does not exist, it will be created. |
wb+ | Open for both reading and writing in binary mode. | If the file exists, its contents are overwritten. If the file does not exist, it will be created. |
a+ | Open for both reading and appending. | If the file does not exists, it will be created. |
ab+ | Open for both reading and appending in binary mode. | If the file does not exists, it will be created. |
fclose:Â This function is used for closing an open file
Syntax: fclose(fp)
f(seek):This function will help to get the required data.
Syntax:Â fseek(FILE * stream, long int offset, int whence)
“FILE* stream” is the pointer to the file.
“long int offset” is the position of the record to be found.
“int whence” specifies the location where the offset starts.There are 3 different whence in SEEK. The details are given below:
SEEK_SET-Starts the offset from the beginning of the file.
SEEK_END-Starts the offset from the end of the file.
SEEK_CUR-Starts the offset from the current location of the cursor in the file.
Note: Before jumping into the source code below we would like to mention that our main goal is to provide you a good packaged solution under one umbrella.Refer to other examples of “C Programming”. For all the possible “C programming questions” which might you have faced in interviews or any other assignments or projects. If you are not able to find the desired solution, please drop us comments so that we can get back to you to with some more and more beautiful programs.You can also check out our ebook below by clicking the image anywhere which will help you for further practice and quick learning.
Note:Â Currently discounts are going on in our books!
Source Code
// Created by Kaustav Ghosh Dastidar. /* Write a menu driven program that will handle an employee file having following record structure: EMPLOYEE [ int EMP_NO(4), char EMP_NAME(30), float EMP_SAL(5.2) ] The operation to be performed on the file are: 1. Add a new record 2. Modify an existing record 3. Delete an existing record 4. Display all record(s). */ #include<stdio.h> void add_record(FILE *); void modify_record(FILE *); void delete_record(FILE *); void display_record(FILE *); typedef struct employee { int no; char name[30]; float sal; }EMP; void main() { FILE *fp; int choice; do { clrscr(); fp=fopen("e:/c/kaustav.txt","rb+"); if(fp==NULL) { fp=fopen("e:/c/kaustav.txt","wb+"); if(fp==NULL) { printf("\n\n Unable to create a file:"); getch(); exit(1); } } printf(" ******** MENU ********\n"); printf(" ------------------------"); printf("\n\n Press 1: Add a new record"); printf("\n\n Press 2: Modify a existing record"); printf("\n\n Press 3: Delete a existing record"); printf("\n\n Press 4: Display all record(s)"); printf("\n\n Press 0: Exit"); printf("\n\n\t Enter your choice: "); scanf("%d",&choice); switch(choice) { case 1: add_record(fp); getch(); break; case 2: modify_record(fp); getch(); break; case 3: delete_record(fp); fclose(fp); remove("e:/c/subhendu.txt"); rename("e:/c/temporary.txt","e:/c/subhendu.txt"); break; case 4: display_record(fp); getch(); break; case 0: printf("\n\n ***** EXIT *****"); break; default:printf("\n\n Invalid choice"); getch(); } }while(choice); fclose(fp); getch(); } /******************* Add new record ***********************/ void add_record(FILE *fp) { EMP e; do { clrscr(); fseek(fp,0,SEEK_END); printf("\n\n Enter the Employee Name: "); scanf("%s",e.name); fflush(stdin); printf("\n\n Enter the Employee No: "); scanf("%d",&e.no); fflush(stdin); printf("\n\n Enter the Employee Salary: "); scanf("%f",&e.sal); fflush(stdin); fwrite(&e,sizeof(e),1,fp); fseek(fp,0,SEEK_SET); printf("\n\n Record is added successfully!"); printf("\n\n Do you want to add another record [Y|N]_"); }while(toupper(getchar())!='N'); } /**************** Dispaly Record ************************/ void display_record(FILE * fp) { EMP e; int count=0; printf("\n\nEmployee_No Name Salary"); printf("\n____________________________________________"); while(fread(&e,sizeof(e),1,fp)) { printf("\n\n %4d \t %20s \t %5.2f",e.no,e.name,e.sal); count++; if(!(count%10)) { printf("\n\n press any key to continue"); getch(); } } } /************** Delete a existing record ***************/ void delete_record(FILE *fp) { FILE *tmp; EMP e; int empno; tmp=fopen("e:/c/temporary.txt","wb+"); if(tmp==NULL) { printf("\n\n Unable to create a temporary file!"); getch(); exit(1); } do { clrscr(); printf("\n\n Enter searching Employee number: "); scanf("%d",&empno); fflush(stdin); while(fread(&e,sizeof(e),1,fp)) { if(e.no!=empno) { fwrite(&e,sizeof(e),1,tmp); } } printf("\n\n Searching element is deleted successfully!"); printf("\n\n Do you want to delete another record [Y|N]_"); }while(toupper(getchar())!='N'); fclose(tmp); } /************** Modify the Record *******************/ void modify_record(FILE *fp) { int empno; EMP e,e1; do { printf("\n\n Give the employee number : "); scanf("%d",&empno); fflush(stdin); fseek(fp,0,SEEK_SET); while(fread(&e,sizeof(e),1,fp)) { printf("\n%d :",e.no); if(e.no==empno) { e1.no=e.no; printf("\n\n Give the new employee name : "); scanf("%s",e1.name); fflush(stdin); printf("\n\n Give the new employee salary :"); scanf("%d",&e1.sal); fflush(stdin); fseek(fp,-(sizeof(e1)),SEEK_CUR); fwrite(&e1,sizeof(e1),1,fp); } } printf("\n\n The desired record is modified!"); printf("\n\n Do you want to continue modification [Y|N]_"); }while(toupper(getchar())!='N'); }