C Append Data to a File Program


Appending data to a file in C allows you to add new content to an existing file without overwriting its current contents. Below is a simple C program that demonstrates how to append data to a file.

Program: Append Data to a File in C

#include <stdio.h> #include <stdlib.h> int main() { FILE *file; char data[100]; // Open the file in append mode ("a") file = fopen("output.txt", "a"); if (file == NULL) { perror("Error opening file"); return EXIT_FAILURE; } // Get user input to append to the file printf("Enter data to append to the file: "); fgets(data, sizeof(data), stdin); // Read a line of input // Write the input data to the file fprintf(file, "%s", data); // Close the file fclose(file); printf("Data appended successfully.\n"); return EXIT_SUCCESS; }

Explanation of the Program

  1. Header Files:

    • #include <stdio.h>: This header file is necessary for input/output functions.
    • #include <stdlib.h>: This header file is included for the exit() function and error handling.
  2. File Pointer:

    • FILE *file;: A pointer variable to handle file operations.
  3. Data Array:

    • char data[100];: An array to store the string input that will be appended to the file.
  4. Open the File:

    • The program opens the file output.txt in append mode ("a"). In append mode, data is added to the end of the file:
      file = fopen("output.txt", "a");
    • If the file cannot be opened (for example, if it does not exist), the program prints an error message using perror() and exits.
  5. Get User Input:

    • The program prompts the user to enter data that they want to append to the file:
      printf("Enter data to append to the file: "); fgets(data, sizeof(data), stdin);
    • fgets() is used to read a line of text from standard input, including spaces, until a newline character or the specified limit (in this case, 100 characters) is reached.
  6. Write to the File:

    • The input data is written to the file using fprintf():
      fprintf(file, "%s", data);
    • This function formats the data and appends it to the end of the file.
  7. Close the File:

    • After the data is successfully written, the program closes the file using fclose():
      fclose(file);
  8. Confirmation Message:

    • Finally, the program prints a success message to inform the user that the data has been appended successfully:
      printf("Data appended successfully.\n");

How to Run the Program

  1. Create or Ensure the Output File:

    • Make sure you have a file named output.txt in the same directory as the program, or the program will create it automatically when opened in append mode.
  2. Compile the Code:

    • Use a C compiler (like gcc) to compile the code:
      gcc append_data.c -o append_data
  3. Execute the Program:

    • Run the compiled program:
      ./append_data
  4. Input Data:

    • Enter the text you want to append to the file when prompted.

Example Output

If the contents of output.txt before running the program are:

This is the first line.

After running the program and entering the text:

This is an appended line.

The contents of output.txt will be:

This is the first line. This is an appended line.

Conclusion

This simple program demonstrates how to append data to an existing file in C using file handling functions. The use of append mode ensures that the existing data in the file remains intact while allowing new data to be added at the end. This technique can be useful for logging, data collection, and many other applications where maintaining historical data is important.