C program to append data to file

Write a C program to read string from user and append data to file. How to append data at end of a file in C programming. How to open file in append mode and append data to file.

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, Pointers, File Handling

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

How to append string to file

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

Step by step descriptive logic to append string to 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) mode and store reference to fPtr using fPtr = fopen(filePath, "a");.
  • Input string from user to append to file, store it to some variable say dataToAppend.
  • Write string data to file using fputs(dataToAppend, fPtr);.

Program to append data to file

/**
 * C program to append string to 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 string 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 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>