im facing a bit of a problem recently. i want to implement my own fopen function for error checking purposes.
Heres the relevant code so far:
enum _Errorcode_ openFile( const char * c_str_filename, const char * c_str_mode, FILE* the_file )
{
the_file = fopen( c_str_filename, c_str_mode );
if( the_file == NULL )
{
return FILE_IO_ERROR;
}
else
{
return OK;
}
}
i call the function like this:
FILE * oFile = NULL;
...
ErrorCode = openFile( "myfile.txt", "r", oFile );
If i check the pointer addr of oFile afterwards, its still pointing to NULL.
The wird thing is, my function returend OK, and no failure. why is that so?
The file exists, if i call the fopen() function just so, everything works.
C passes parameters by value, so you assign to a copy of oFile, that’s why you don’t see the change outside of
openFile().Pass a pointer to it, like so: