Write to a File and Then Read from That File - C File

C examples for File:File Operation

Description

Write to a File and Then Read from That File

Demo Code

#include <stdio.h>

int main(){/*  w  w w .  j a  v  a 2s  .co  m*/
     FILE *fptr;
     char store[80];
     int k;
     fptr = fopen("C:\\Code\\pune.txt", "w+");
    
     if(fptr != NULL){
          puts("File opened successfully");
          fputs("test.", fptr);
          rewind(fptr);
          fgets(store, 80, fptr);
          puts("Contents of file pune.txt:");
          puts(store);
          k = fclose(fptr);
          if(k == -1)
              puts("File-closing failed");
          if(k == 0)
              puts("File closed successfully");
     }else
           puts("File-opening failed");

 return(0);
}

Result


Related Tutorials