C program to add new line in existing file fprintf()

C program to add new line in existing file.



Program:

#include <stdio.h>
/* C program to add new line in existing file by SlashMyCode.com */
int main()
{
   char sentence[1000];
   FILE *fptr;

   fptr = fopen("file.txt", "w"); /*To open file*/

   printf("Enter a sentence:\n");
   gets(sentence);

   fprintf(fptr,"%s", sentence); /*To write in a file*/
   fclose(fptr); /*To close file*/
/* C program to add new line in existing file by SlashMyCode.com */
   getch();
}


NOTE: Program will create new file if file doesn't exist in directory. 


Output:

as you can see here, the sentence entered in program has been added in file.txt
output of fprintf in c






No comments:

Post a Comment

Feel free to ask us any question regarding this post