Determining if a file being inputted and read is really empty

62 Views Asked by At

I have written this void function to check input files being read are valid, but there is one thing I don't know how to implement. fseek and ftell are used to determine if a file is empty or not, but if a file is full of spaces or tabs, it will count as not being empty (though it is).

The input files will either contain integers and/or word strings. So is there a way to check if the whole file has any ints or strings or otherwise only contains blank spaces?

I imagine I might have to use either scanf_s or gets, with isspace...

FILE *inp;

/* Checks to see if file can be read */
errCode = fopen_s(&inp, file_location, "r");
if (errCode != 0) {
    printf(" ERROR. File %s was not located, ensure the following directory is valid: %s\n\n ", file_name, file_location);
    *file_Error = ERROR;
    return;
}

/*Move to end of file*/
fseek(inp, 0, SEEK_END);

/*Set filesize to size at end of file*/
fileSize = ftell(inp);

/*Print error and exit if filesize is zero i.e. contains 0 bytes (no data)*/
if (fileSize == 0) {
    fclose(inp);
    printf(" ERROR. File %s contains no data. (File size = %d bytes)\n\n ", file_name, fileSize);
    *file_Error = ERROR;
    return;
}

/*Print error and exit if filesize is zero for .dats containing only spaces or \t i.e. contains 0 bytes (no data)*/

/*Reset to beginning of file for reading*/
fseek(inp, 0, SEEK_SET);

/* Checks for error reading file occured */
if (errCode != 0) {
    printf(" Error opening file %s.\n\n ", file_name);
    *file_Error = ERROR;
    return;
}

/*if no error on file*/
else
{
    printf_s(" File %s Opened.\n\n", file_name);
    *file_Error = 0;
}
0

There are 0 best solutions below