In this related question How to use feof and ferror for fgets (minishell in C), the answers aren't really clear for my problem.
What I'm trying to do is: I want to read all of the characters from a plain text file on my disk. It's not a link, devicefile, socket etc. just a regular text file. Once I've read all the characters I want to see if everything succeeded. What I'm seeing now is, in my Debug builds everything goes successfully, but in my Release builds ferror()
always indicates there is a an error. While the Debug build returns the 0 in the end. In both cases I can see that the content of the file has been obtained in a debugger.
my_function(File* f) {
int c;
while ((c = fgetc(f)) != EOF) {
char character = (char) c;
// store character in a dynamic growing buffer
}
// append '0' byte to buffer (to terminate the string).
if (ferror(f)) {
// return error
return 1;
}
return 0; // no error.
}
Rationale of having a already opened file as function argument, is to make it relatively easy to read from a file, without the need to bother with a platform dependent encoding. This is inside a private part of my library, the public functions handle the case that f == NULL
.
Do I need to call clearerr(file)
first, because the error bit is not initialized in a Release builds?