Why does this shit return a (FILE *)?
- The oneFILE*variable gets its previously held file closed, all good.
- The oneFILE*variable gets loaded with the "asd.txt" with "r+" attribute, all good.
Then the function fucking returns the same pointer, again, the same thing that has been fucking stored inside the oneFILE*variable, why?
freopen_s is Microsoft's idea. No idea why they did that, but MS used macros/templates/overloads to convert normal C++ code to use the functions so they probably wanted the conversion to be as mechanical as possible. Since freopen could fail, it can return NULL.
Name:
Anonymous2014-02-08 17:59
>>1
freopen_s is a unportable shit that doesn't have reasont to exist. I think it's in the C11 standard as optional thing but only MS implements it because they were support it before the standard
Name:
Anonymous2014-02-08 19:11
>>3
freopen returning NULL when an error occurs is a good reasoning for it to have a return value, but why a return value with the 4 byte type (FILE *)? A single byte (char) would be able to do it.
Whatever, I think I'll just use freopen then, and disregard the return value, or maybe use it within "if". I don't want to create a dummy (FILE *) to pass into freopen_s.
What reply codes are here in this board? /b/ has none... code tags perhaps
>>5 A single byte (char) would be able to do it.
Using smaller types than int or char * for function return values is a silly optimization (you'll notice that none of the functions in the standard library do it). On most architectures, a whole register is used for the return value regardless of whether you're returning a char, an int, or a char *.
I think I'll just use freopen then, and disregard the return value
...and then your program will die horribly if anything bad happens to the file after you open it the first time.
Name:
Anonymous2014-02-09 9:15
>...and then your program will die horribly if anything bad happens to the file after you open it the first time.
Explain... I am planning to use something like this:
if ( freopen( "some.txt", "r+", someFILEpointer ) ) {
/* do things with the file */
}
else {
printf_s( "Your file could not be opened for writing" );
/* don't do things with the file */
}
What bad is to happen to my file? I don't get it...
>>7
Usually when somebody says they are ignoring a return value it means that they are doing nothing with it (e.g. that the resulting program would behave the same if the return value of the function whose argument is ignored is cast to void).
You are testing the return value for null there; that is perfectly fine.
>>8
And in a case where all that's really needed is puts!
Name:
Anonymous2014-02-10 14:52
>>9
Well, you're right, I should have been more explicit about what I'm ignoring.
What I was trying to say is that I will be ignoring it as a FILE pointer, while still using it a simple error indicator.