C program to append data into a file

Write a C program to read data from user and append data into a file. How to append data at end of a file in C programming. In this post I will explain append mode in file handling. I will cover how to append data into a file in C using append file mode.

Example

Source file content

I love programming.
Programming with files is fun.

String to append

Learning C programming at Codeforwin is simple and easy.

Output file content after append

I love programming.
Programming with files is fun.
Learning C programming at Codeforwin is simple and easy.

Required knowledge

Basic Input Output, Do while loop, Pointers, File Handling

In previous two posts, I explained to create a file and write data into file and how to read a file. In this post we will continue further and learn to append data into a file.

How to append data into a file?

C programming supports different file open mode to perform different operations on file. To append data into a file you can use a file open mode.

Step by step descriptive logic to append data into a file.

  • Input file path from user to append data, store it in some variable say filePath.
  • Declare a FILE type pointer variable say, fPtr.
  • Open file in a (append file) mode and store reference to fPtr using fPtr = fopen(filePath, "a");.
  • Input data to append to file from user, store it to some variable say dataToAppend.
  • Write data to append into file using fputs(dataToAppend, fPtr);.
  • Finally close file to save all changes. Use fclose(fPtr);.

Program to append data into a file

/**
 * C program to append data to a file
 */

#include <stdio.h>
#include <stdlib.h>

#define BUFFER_SIZE 1000


void readFile(FILE * fPtr);


int main()
{
    /* File pointer to hold reference of input file */
    FILE *fPtr;
    char filePath[100];

    char dataToAppend[BUFFER_SIZE];


    /* Input file path to remove empty lines from user */
    printf("Enter file path: ");
    scanf("%s", filePath);

    /*  Open all file in append mode. */
    fPtr = fopen(filePath, "a");


    /* fopen() return NULL if unable to open file in given mode. */
    if (fPtr == NULL)
    {
        /* Unable to open file hence exit */
        printf("\nUnable to open '%s' file.\n", filePath);
        printf("Please check whether file exists and you have write privilege.\n");
        exit(EXIT_FAILURE);
    }


    /* Input data to append from user */
    printf("\nEnter data to append: ");
    fflush(stdin);          // To clear extra white space characters in stdin
    fgets(dataToAppend, BUFFER_SIZE, stdin);


    /* Append data to file */
    fputs(dataToAppend, fPtr);


    /* Reopen file in read mode to print file contents */
    fPtr = freopen(filePath, "r", fPtr);

    /* Print file contents after appending string */
    printf("\nSuccessfully appended data to file. \n");
    printf("Changed file contents:\n\n");
    readFile(fPtr);


    /* Done with file, hence close file. */
    fclose(fPtr);

    return 0;
}



/**
 * Reads a file character by character 
 * and prints on console.
 * 
 * @fPtr    Pointer to FILE to read.
 */
void readFile(FILE * fPtr)
{
    char ch;

    do 
    {
        ch = fgetc(fPtr);

        putchar(ch);

    } while (ch != EOF);
}

data/append.txt file contents before appending string.

I love programming.
Programming with files is fun.

Output

Enter file path: data\append.txt

Enter data to append: Learning C programming at Codeforwin is simple and easy.

Successfully appended data to file.
Changed file contents:

I love programming.
Programming with files is fun.
Learning C programming at Codeforwin is simple and easy.

Happy coding 😉