Contact Learn C
Copy Program Share Program

Program 204:To Append Text into a File

Program 204:
 
#include<stdio.h>
#include<stdlib.h>
 main ()
 {
    FILE *file;
    char c;
    file=fopen("file3append.txt","w");
 fprintf(file,"%s","Hello!");
    fclose(file);
    
    file=fopen("file3append.txt","r");
     printf("After writing\n");
    //To display after write operation to file
    while(1)
    {
     if(file==NULL)
     {
      printf("File Not Found\n");
      exit(0);
     }
     else
     {
      c=fgetc(file);
      if(c==EOF)
      {
       break;
      }
      printf("%c",c);
     }
    }
    fclose(file);
    printf("\n");
    file=fopen("file3append.txt","a");//opening file in append mode    
 fprintf(file,"%s","World");
    fclose(file); 
    file=fopen("file3append.txt","r");
    printf("After appending\n");
    //To display after append operation to file
    while(1)
    {
     if(file==NULL)
     {
      printf("File Not Found\n");
      exit(0);
     }
     else
     {
      c=fgetc(file);
      if(c==EOF)
      {
       break;
      }
      printf("%c",c);
     }
    } 
 fclose(file);
 printf("\n");  
 }
Explanation:

//Coming Soon...

Output:

Store both text file and program in same folder.Here I named text file as file3append.txt

Running the Program





After Running the program




 

No comments:

Post a Comment

Donate

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide