Read a Text File String by String using the function fgets() - C File

C examples for File:Text File

Description

Read a Text File String by String using the function fgets()

Demo Code

#include <stdio.h>
int main()//from www  .j  a  va 2 s.c o m
{
     int k = 0;
     char *cptr;
     char store[80];
     FILE *fptr;
     fptr = fopen("C:\\Code\\data.txt", "r");
     if (fptr != NULL) {
          puts("File is opened successfully.");
          cptr = fgets(store, 80, fptr);
        
          while (cptr != NULL) {
           printf("%s", store);
           cptr = fgets(store, 80, fptr);
          }
        
          k = fclose(fptr);
          if(k == -1)
              puts("File-closing failed");
          if(k == 0)
              puts("\nFile is closed successfully.");
     }
     else
      puts("File-opening failed");
     return(0);
}

Result


Related Tutorials