In this article, we will write a C program to append data to an existing file. The program asks the user to enter the name of the file to which he wants to append the data, opens it in append mode, and again asks the user to enter the data he wants to append to the file and successfully appends it to the existing file.
If some error occurs while opening or appending data to the file, it prompts an error to the user.
Sample Input:
Enter the name of the file: myfile.txt Enter the data to append to the file: This is some random content to append to the file
Sample Output:
Data appended to the myfile.txt file successfully
In C programming language, if you want to append data to an existing file, you have to first open the existing file in append mode("a"
) using the fopen() function, then take the data from the user using the fgets() function and finally use the fputs() function to append data to the file.
The fopen()
function basically takes two arguments, the name of the file which we want to open and the mode in which we want to open the file.
It has the following syntax:
FILE *fopen(const char *filename, const char *mode);
As we want to append the data to the file, therefore, we have to pass the mode as "a"
which denotes the append mode.
The fopen()
function returns a FILE
pointer on opening the file. If this FILE
pointer is NULL
, it means there is some error opening the file, otherwise, the file is opened successfully.
See implementation in the following program:
// C program to append data // to an existing file #include <stdio.h> int main(){ char filename[100], data[1000]; FILE *fptr; printf("Enter the name of the file: "); scanf("%s", &filename); // Open file in append mode fptr = fopen(filename, "a"); if(fptr == NULL){ printf("Error opening the file"); return 1; } printf("Enter the data to append to the file: \n"); fflush(stdin); // Consume the newline character left in input buffer // Get the data from the user fgets(data, 1000, stdin); // Append the data to the file fputs(data, fptr); printf("Data appended to the %s file successfully", filename); // Close the file fclose(fptr); return 0; }
Output:
Enter the name of the file: myfile.txt Enter the data to append to the file: This is some random content to append to the file Data appended to the myfile.txt file successfully
Here is a screenshot of myfile.txt after appending the data to it:
Explanation of the Program
Here is a detailed explanation of the above program:
- The program starts by prompting the user to enter the name of the file to which he wants to append data. The file name entered by the user is stored in the
filename
character array. - The program declares a file pointer
fptr
of typeFILE
that will be used to work with the file. - The program opens the file specified by
filename
in append mode ("a"
) usingfopen()
. - Append mode allows data to be added to the end of an existing file, or a new file is created if it doesn’t exist:
- Error checking is performed to ensure that the file was successfully opened. If
fptr
isNULL
, it indicates an error - The user enters the data, which is read and stored in the
data
character array usingfgets()
with a maximum input size of 1000 characters. - The program appends the user-entered data to the file using the
fputs()
function. - Finally, the program closes the file using
fclose(fptr)
to release system resources.
I hope you like this article. Thanks for reading.