3

I want to check if my text file contains data or not. If the file contains a data I want to read it. My problem is I don't know the right condition to write in the if statement. Hint: I tried to use the functions of fseek & ftell, but without any benefit. I want to know why this condition in the if statement doesn't work properly?

    FILE *fptr;
  if(ftell(fptr)!=0){   //check if the file is not empty.
    if ( !( fptr = fopen( "saving.txt", "r" ))){
        printf( "File could not be opened to retrieve your data from it.\n" );
    }
    else{
        while ( !feof( fptr ) ){
            fscanf( fptr, "%f\n", &p.burst_time );
            AddProcess(&l,p.burst_time);
        }
        fclose( fptr );
    }
   }

3 Answers3

6

it does'nt work because you have to fopen it and fseek to the end first:

FILE *fptr;
if ( !( fptr = fopen( "saving.txt", "r" ))){
    printf( "File could not be opened to retrieve your data from it.\n" );
}
else {
    fseek(fptr, 0, SEEK_END);
    unsigned long len = (unsigned long)ftell(fptr);
    if (len > 0) {  //check if the file is empty or not.
        rewind(fptr);
        while ( !feof( fptr ) ){
            fscanf( fptr, "%f\n", &p.burst_time );
            AddProcess(&l,p.burst_time);
        }
    }
    fclose( fptr );
}
h3n
  • 880
  • 1
  • 10
  • 26
  • It works well in knowing if the file is empty or not, but it affects on reading from file. It makes fscanf reads something like garbage. When I remove these lines of code it reads well from the file: fseek(fptr, 0, SEEK_END); unsigned long len = (unsigned long)ftell(fptr); if (len > 0) { //check if the file is empty or not. Please help me. – Hossam Atef El-Esseily May 08 '15 at 22:51
  • you might want to seek back to the beginning using rewind(fptr); before reading it, yup – h3n May 08 '15 at 22:57
  • Unfortunately. It doesn't work yet. – Hossam Atef El-Esseily May 08 '15 at 23:09
  • Please, write the whole correct code. – Hossam Atef El-Esseily May 08 '15 at 23:10
  • I tested the code, it works with my file containing just floats. So please post the content of you file. – h3n May 08 '15 at 23:30
  • my file before: fseek(fptr, 0, SEEK_END); unsigned long len = (unsigned long)ftell(fptr); if (len > 0) { //check if the file is empty or not. is: 1.000000 and after: 1.000000 3772238817795027700000000000000000.000000 – Hossam Atef El-Esseily May 08 '15 at 23:36
  • [SEEK_END](http://www.cplusplus.com/reference/cstdio/fseek/) seems non-portable – maidamai Sep 02 '20 at 02:48
3

Call ftell() does not tell you the size of the file, it tells you the current value of the file position indicator which, when you first open the file, will always be 0.

Using sys/stat.h and call the value of the st_size member, if its 0, your file is empty.

So basically,

#include <sys/stat.h>
char* filename; //I'm assuming this was defined somewhere earlier in your code
FILE *fptr;
struct stat fileStat;
stat(filename, &fileStat)

if( fileStat.st_size !=0 ){   //check if the file is not empty.
    if ( !( fptr = fopen( "saving.txt", "r" ))){
        printf( "File could not be opened to retrieve your data from it.\n" );
    }
    else{
        while ( !feof( fptr ) ){
            fscanf( fptr, "%f\n", &p.burst_time );
            AddProcess(&l,p.burst_time);
        }
    fclose( fptr );
}

}

Hope this helps.

JGrindal
  • 793
  • 2
  • 8
  • 23
  • FILE *fptr; if ( !( fptr = fopen( "saving.txt", "r" ))){ printf( "File could not be opened to retrieve your data from it.\n" ); } else { fseek(fptr, 0, SEEK_END); unsigned long len = (unsigned long)ftell(fptr); if (len > 0) { //check if the file is empty or not. rewind(fptr); while ( !feof( fptr ) ){ fscanf( fptr, "%f\n", &p.burst_time ); AddProcess(&l,p.burst_time); } } fclose( fptr ); } It tells me if the file is empty or not, but it affects on reading from file. It makes fscanf reads something like garbage. – Hossam Atef El-Esseily May 08 '15 at 23:24
1

You can use fseek to the end, then check if ftell returns 0.

bool isEmpty(FILE *file){
    long savedOffset = ftell(file);
    fseek(file, 0, SEEK_END);

    if (ftell(file) == 0){
        return true;
    }

    fseek(file, savedOffset, SEEK_SET);
    return false;
}

or you can use sys/stat.h and call the value of the st_size struct member:

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

int main (int argc, char *argv[]) {
   if (argc != 2) {
   return EXIT_FAILURE;
   }

   const char *filename = argv[1];
   struct stat st;

   if (stat(filename, &st) != 0) {
       return EXIT_FAILURE;
   }
   fprintf(stdout, "file size: %zd\n", st.st_size);
   return EXIT_SUCCESS;
}
Ziezi
  • 6,375
  • 3
  • 39
  • 49