Write to a Text File in Interactive Mode using the function fputs() - C File

C examples for File:Text File

Description

Write to a Text File in Interactive Mode using the function fputs()

Demo Code

#include <stdio.h>
#include <string.h>

int main()/* w  w w.  j a  v a  2 s . c  om*/
{
     int k = 0, n = 0;
     char filename[40], temp[15], store[80];
     FILE *fptr;
     printf("filename: ");
     scanf("%s", temp);
     strcpy(filename, "C:\\Code\\");
     strcat(filename, temp);
     fptr = fopen(filename, "w");
     if (fptr != NULL) {
          printf("File %s is opened successfully.\n", filename);
          puts("test.");
          puts("test.");
          fflush(stdin);
          gets_s(store);
          n = strlen(store);
        
          while(n != 0){
               fputs(store, fptr);
               fputs("\n", fptr);
               gets_s(store);
               n = strlen(store);
          }
        
          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