0

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;
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Scuba
  • 38
  • 6
  • 4
    The only way to see if a non-empty file contains valid data is to read and validate the data. – Some programmer dude Oct 07 '16 at 09:38
  • 1
    Your code is overly complicated, just read the and validate the data as you are reading. There is no need to handle the case of an empty file (one of length 0). – Jabberwocky Oct 07 '16 at 10:08
  • Yeah that's what I have in my program, but one of the markers for this project said I should have it in my initial input file check function. – Scuba Oct 07 '16 at 10:19

0 Answers0