Explain append mode operation of files in C language

In C programming, append mode is a file opening mode that allows you to add new data to the end of an existing file without overwriting its current contents. When you open a file in append mode, the file pointer is automatically positioned at the end of the file.

Syntax

FILE *fp;
fp = fopen("filename.txt", "a");

Where "a" represents the append mode. If the file doesn't exist, a new file will be created. If the file exists, new data will be added after the existing content.

Key Features of Append Mode

  • File pointer is positioned at the end of the file
  • Existing content is preserved
  • New data is added after existing content
  • Creates a new file if it doesn't exist
  • Only allows write operations

Example 1: Basic Append Operation

This example demonstrates how to append text to a file −

#include <stdio.h>

int main() {
    FILE *fp;
    
    /* Open file in append mode */
    fp = fopen("sample.txt", "a");
    
    if (fp == NULL) {
        printf("Error opening file!<br>");
        return 1;
    }
    
    /* Append data to file */
    fprintf(fp, "This line will be appended.<br>");
    fprintf(fp, "Another line appended.<br>");
    
    /* Close the file */
    fclose(fp);
    
    printf("Data appended successfully!<br>");
    return 0;
}

Example 2: Reading and Appending Data

This example shows how to read existing content and then append new data −

#include <stdio.h>

int main() {
    FILE *fp;
    char buffer[100];
    
    /* First, read existing content */
    fp = fopen("data.txt", "r");
    if (fp != NULL) {
        printf("Existing content:<br>");
        while (fgets(buffer, sizeof(buffer), fp) != NULL) {
            printf("%s", buffer);
        }
        fclose(fp);
    }
    
    /* Now append new content */
    fp = fopen("data.txt", "a");
    if (fp == NULL) {
        printf("Error opening file for append!<br>");
        return 1;
    }
    
    fprintf(fp, "New line 1<br>");
    fprintf(fp, "New line 2<br>");
    
    fclose(fp);
    printf("\nData appended successfully!<br>");
    return 0;
}

Example 3: Append Mode vs Write Mode

This example demonstrates the difference between append mode and write mode −

#include <stdio.h>

int main() {
    FILE *fp;
    
    /* Create initial file with write mode */
    fp = fopen("test.txt", "w");
    if (fp != NULL) {
        fprintf(fp, "Initial content<br>");
        fclose(fp);
        printf("Initial file created.<br>");
    }
    
    /* Append to the file */
    fp = fopen("test.txt", "a");
    if (fp != NULL) {
        fprintf(fp, "Appended content<br>");
        fclose(fp);
        printf("Content appended.<br>");
    }
    
    /* Read and display final content */
    fp = fopen("test.txt", "r");
    if (fp != NULL) {
        char line[100];
        printf("\nFinal file content:<br>");
        while (fgets(line, sizeof(line), fp) != NULL) {
            printf("%s", line);
        }
        fclose(fp);
    }
    
    return 0;
}

Common File Modes Comparison

Mode Purpose File Position Creates New File
"r" Read only Beginning No
"w" Write only Beginning (truncates) Yes
"a" Append only End of file Yes
"a+" Read and append End for writing Yes

Key Points

  • Always check if file opening was successful by comparing with NULL
  • Use fclose() to properly close the file after operations
  • Append mode preserves existing data, unlike write mode which truncates
  • File position cannot be changed with fseek() in append mode on some systems

Conclusion

Append mode in C is essential for adding new data to existing files without losing original content. It's commonly used for logging, data collection, and maintaining records where preservation of existing data is crucial.

Updated on: 2026-03-15T13:52:56+05:30

791 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements