Write to a Text File Character by Character using the function fputc() - C File

C examples for File:Text File

Description

Write to a Text File Character by Character using the function fputc()

Demo Code

#include <stdio.h>

int main(){/*from   www .j  a  va 2  s  .  co  m*/
     int k = 0, n = 0;
     FILE *fptr;
     fptr = fopen("C:\\Code\\jaipur.txt", "w");
     if (fptr != NULL) {
          puts("File is opened successfully.");
          puts("Enter * to terminate the text-entry-session.");
          n = getchar();
          while(n != '*'){
              fputc(n, fptr);
              n = getchar();
          }        
          k = fclose(fptr);
          if(k == -1)
              puts("File-closing failed");
          if(k == 0)
              puts("File is closed successfully.");
     }
     else
          puts("File-opening failed");
     return(0);
}

Result


Related Tutorials