C Program to Append / Add More Records in Binary File

By | June 27, 2019

To day we will learn to write a “C Program to Append / Add More Records in Binary File”. Let us study some important concepts related to appending a file in C Programming.

C Program to Append / Add More Records in Binary File

C Program to Append / Add More Records in Binary File

What Do You Mean By Appending a File?

By Appending a file we mean adding More records at the end of a binary file. This is known as the file operation “Append”.

In Append operation the existing records remain in the binary file. The user can enter more records in the same binary file, too.

The difference Between Write Mode and Append Mode

The main difference between Write mode and Append mode is as follows:

If we use write mode that is ‘wb’, the file will be created (first time)  or recreated ( next time ). Whereas, if we use ‘append ‘file opening mode that is ‘ab’ mode then the existing file will be opened. So that we can add more records at the end of file.

Anxiety behind Write File Open Mode

The file opening mode ‘Write’ can be dangerous and can destroy your file data. This happens if you open the binary file in ‘wb’ mode again. If we open a file in write mode for first time, the new file is created. And if we reopen a file in ‘wb’ mode, the file will be overwritten by a new empty file with the same name by deleting the old one.

How to write code for C Program to Append / Add More Records in Binary File

Fortunately, there is no much effort to write an append file program.   This website www.easycodebook.com already has a C program to write records in a binary file. You will use the same C program to add more records but with only one difference. We will use file open function as:

fptr = fopen(“filename”,”ab”); to append more records instead of

fptr = fopen(“filename”, “wb”);

Note that if we already use fopen(“filename”,”ab”); then there will be no change in the program. Since “ab” file open mode can create file for first time. And it is used for appending records for next time.

Source Code for C Program for adding more records in existing file:
/* Write a C Program to add more records at
end of already existing binary file
that is append file operation
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct student
{
int rollno;
char name[30];
}srecord;


int main()
{
	
FILE *fp;

char ans;
int i;

/*opening binary file in appending / writing mode*/
fp=fopen("d://sfile.dat","ab");	
if(fp==NULL)
{
printf("File could not open");
exit(0);
}

while(1)
{
printf("\nEnter Student Record\n");
printf("Roll No:");
scanf("%d",&srecord.rollno);
getchar();
printf("Name:");
gets(srecord.name);
/* write a record to binary file */
fwrite(&srecord,sizeof(srecord),1,fp);
printf("\nRecord has been added successfully");

printf("\nAdd more records? press y or n =");
ans=getche();
if(ans=='n' || ans=='N')
break;
}
fclose(fp);
printf("\nProgram ended: C Program To Add more Student Records in Binary File:");
return 0;
}

We will compile and execute this program. As we know in previous ‘C program to Write records in binary file‘ we have added records of two students Rool No: 1 and Roll No:2.

C Program to add more records in binary file

C Program to add more records in binary file

Now we will add two more records for student Roll No:3 and 4. We can use check these records by executing already written program ‘ C program to read all records from existing binary file‘.

c-program-to-append-add-more-records-in-binary-file-output-display-all-records-

c-program-to-append-add-more-records-in-binary-file-output-display-all-records-

Loading

One thought on “C Program to Append / Add More Records in Binary File

  1. Pingback: Python File Phonebook Program | EasyCodeBook.com

Leave a Reply

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