Appending to a file means adding at the end of the file while keeping the previous contents of the file intact. For this the file open mode is “a”. Program illustrates appending a file.
#include <stdio.h> #include <stdlib.h> void main() { FILE* fptr; char ch; fptr = fopen("File1.txt", "w"); clrscr(); if(fptr ==NULL) { printf("File1 could not be opened."); exit (1); } else { printf("File1 is open. Enter a sentence.\n"); while ((ch= getchar()) != EOF) fputc(ch, fptr); fclose(fptr); } fptr = fopen("File1", "a"); if (fptr == NULL) { printf("File1 could not be opened"); exit(1); } else { printf("File1 is open for appending.\nWrite a sentence.\n"); while ((ch= getchar()) != EOF) fputc(ch, fptr); fclose(fptr); } fptr = fopen("File1", "r"); printf("After appending contents of File1 are as below.\n"); while ((ch = fgetc (fptr)) != EOF) printf("%c",ch); fclose(fptr); }