Read Integers Stored in a Text File using the function fscanf() - C File

C examples for File:Text File

Description

Read Integers Stored in a Text File using the function fscanf()

Demo Code

#include <stdio.h>

int main(){/*from   w ww .  ja v  a2 s.  c  om*/
     int m = 0, n, k = 0;
     FILE *fptr;
     fptr = fopen("C:\\Code\\numbers.dat", "r");
     if (fptr != NULL) {
          puts("File numbers.dat is opened successfully.");
          m = fscanf(fptr, "%d", &n);
          while(m != EOF){
               printf("%d ", n);
               m = fscanf(fptr, "%d", &n);
          }
          printf("\n");
          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