C Append to Files
Appending to a file means adding new data to the end of an existing file. In C, you append by opening the file with append mode, usually "a", and then writing through a FILE * pointer.
Append mode is useful for logs, reports, score lists, and any file where old data should stay and new data should be added after it.
Open A File In Append Mode
Use fopen with mode "a" to open a text file for appending. If the file does not exist, C tries to create it. If it already exists, the old contents are kept and new writes go at the end.
This is the main difference between "w" and "a": write mode clears the file, while append mode keeps the file.
Append Text With fputs
The fputs function writes a string to a file. When the file is opened with "a", that string is added after the current file contents.
#include <stdio.h>
int main(void)
{
FILE *file = fopen("log.txt", "w");
if (file == NULL) {
printf("Could not create file.\n");
return 1;
}
fputs("Program started\n", file);
fclose(file);
file = fopen("log.txt", "a");
if (file == NULL) {
printf("Could not open file.\n");
return 1;
}
fputs("User logged in\n", file);
fclose(file);
file = fopen("log.txt", "r");
if (file == NULL) {
printf("Could not read file.\n");
return 1;
}
char line[80];
printf("Log file:\n");
while (fgets(line, sizeof(line), file) != NULL) {
printf("%s", line);
}
fclose(file);
return 0;
}
Output:
Log file:
Program started
User logged in
The example first creates a file with one line. It then opens the same file with "a" and adds another line to the end. The final read is included only so you can see the file contents on the screen.
Append Formatted Data With fprintf
Use fprintf when you want to append formatted values, such as numbers, names, or table rows. It works like printf, but its first argument is the file pointer.
#include <stdio.h>
int main(void)
{
FILE *file = fopen("sales.csv", "w");
if (file == NULL) {
printf("Could not create file.\n");
return 1;
}
fprintf(file, "item,quantity\n");
fclose(file);
file = fopen("sales.csv", "a");
if (file == NULL) {
printf("Could not open file.\n");
return 1;
}
fprintf(file, "%s,%d\n", "Pencils", 12);
fprintf(file, "%s,%d\n", "Notebooks", 5);
fclose(file);
file = fopen("sales.csv", "r");
if (file == NULL) {
printf("Could not read file.\n");
return 1;
}
char line[80];
printf("Sales report:\n");
while (fgets(line, sizeof(line), file) != NULL) {
printf("%s", line);
}
fclose(file);
return 0;
}
Output:
Sales report:
item,quantity
Pencils,12
Notebooks,5
This pattern is common when a program needs to add one record at a time. The file can grow over many program runs because each run can append new rows instead of replacing the old rows.
Check For Errors
Always check whether fopen returned NULL before writing. A file might fail to open because of permissions, an invalid path, or another system error.
You should also close the file with fclose when you are done. Closing the file finishes pending writes and releases the file resource.
Append Mode Options
| Mode | Meaning |
|---|---|
"a" |
Open a text file for appending. Create it if it does not exist. |
"a+" |
Open a text file for reading and appending. Writes still go to the end. |
"ab" |
Open a binary file for appending. |
For beginner programs, "a" is the most common choice. If you need to read the file after appending, a simple approach is to close it and reopen it with "r", as shown in the examples.
Compare Write And Append
The next example shows the difference directly. The file is created with "w", then reopened with "a" so the second line is added instead of replacing the first line.
#include <stdio.h>
int main(void)
{
FILE *file = fopen("notes.txt", "w");
if (file == NULL) {
printf("Could not create file.\n");
return 1;
}
fputs("First note\n", file);
fclose(file);
file = fopen("notes.txt", "a");
if (file == NULL) {
printf("Could not open file.\n");
return 1;
}
fputs("Second note\n", file);
fclose(file);
printf("Notes saved.\n");
return 0;
}
Output:
Notes saved.
After this program runs, notes.txt contains both lines. If the second fopen used "w" instead, First note would be erased.
Important Rules
- Include
<stdio.h>before using file functions. - Use mode
"a"when you want to keep old data and add new data at the end. - Use
fprintffor formatted appends andfputsfor simple strings. - Check whether
fopenreturnedNULL. - Call
fclosewhen you are finished writing. - Remember that
"w"overwrites, but"a"appends.
The key idea is that append mode lets a C program add new information to a file without deleting what is already there.
