Saturday, March 25, 2017

C program to append data to text file.

This C program will add message entered by user into the data.txt file. Note that, present content will remain as it is, and new content will be appended at the bottom of data.

#include <stdio.h>
#include <string.h>
int main()
{
   FILE *fp;
   char str[80];
 
   fp = fopen("data.txt", "a");
 
   printf("Enter your message:");
   gets(str);
 
   fprintf(fp, "%s",str);
 
   printf("Your message is appended in data.txt file.");
   fclose(fp);
   //File validation is to be added..
   return 0;
}


Output of program

Enter your message:How are you?
Your message is appended in data.txt file.

Original content of data.txt file is: Hello student.

After execution of program, content of data.txt file: Hello student. How are you?

No comments:

Post a Comment