I'm working on a large project, trying to eliminate warnings. One of the most common warnings that crops up is that the return value from fread isn't checked. fread and kin are used so ubiquitously it would be unwise for me to go through and convert all the files to a c++ stream idiom at this point.
To work around the warnings, I've written an fread wrapper that checks that the return value is correct:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Write an error message and exit
void error(string message)
{
cout << message << endl;
exit(-1);
}
// Error-checking fread wrapper function
size_t fileRead(void* ptr, size_t size, size_t count, FILE* stream)
{
size_t returnCount = fread(ptr, size, count, stream);
if (returnCount != count)
error("Did not read the correct number of fields");
}
This works in most cases but I've run into a hitch.
While linking I get this error:
undefined reference to `fileRead(void*, unsignedlong, unsignedlong, _IO_FILE*)'
The error comes up on calls of the following form:
Could the fact that I'm calling the wrapper from within a namespace cause this error? In some files it links correctly and in others it does not so I'm wondering if that could be the problem.