Conditionals using filehandles in C++
I'm working with a C++ library which makes extensive use of constructs like:
FILE *out_file1, *out_file2 ... *out_fileN;
//for some output files, but not all:
out_file1 = fopen( filename, "w" )
//later
if( out_file1 ) fprintf( ... )
if( out_file2 ) fprintf( ... )
This seems to work OK under g++ on OS X. When I run it on linux, however, I get segfaults. Checking through the code, out_file is often initialised to non zero values.
I've tried adding
out_file = NULL
but this doesn't seem to help - in fact, according to the debugger, it doesn't change the value of out_file.
Can anyone help as to:
Is this a recognised and sensible way to do file IO (i.e. using the file pointers in conditionals)
Why is the value of the pointer not being set to null?
How can I set it to null?
Just to be clear - I'm trying to change the code as little as possible, as I'm coding a wrapper to someone else's library. So, even if the general structure is a strange way to do things, I'd rather find a workaround which doesn't change it if possible.
EDIT: Since this seems to be a reasonable, if outdated way to do conditional file IO, I can narrow the scope of my question to the second two out of three, i.e.
class IO
{
private:
FILE* opFile
IO()
{
//At this point, opFile == 0x40
opFile = NULL; //At this point opFile is still 0x40
开发者_开发百科}
}
So obviously, if it comes out of the constructor with a non-null value, anything like:
if( opFile ) fprintf( ... )
will fail. But how is it managing to come out of the constructor with a non-null value?
And in case it helps, this works "as expected" under gcc on OSX, but not g++-4.3 or g++4.4 on Ubuntu.
Your problem is elsewhere in the code, most likely in the *printf calls you mention?
Show us more code, or use a debugger to find where it crashes.
g++ -O0 -Wall -g mysource.cpp -o test
gdb ./test
(gdb) run argument1 argument2
Also, look at valgrind for additional memory checking tools
valgrind ./test
$0.02
Update
Added -O0 to avoid confusing analysis with results of proper compiler optimization :)
in C++, you should use iostreams, will help you avoid all these issues...
std::ifstream in ("some_file");
if (in)
{
// do stuff with stream...
}
Is this the actual code from your program?
FILE* out_file1, out_file2 ... out_fileN
Then only out_file1
is a FILE*
and all the rest are just FILE
. That would explain their "funny values".
In C++ you should use std::fstream
(or std::istream
/std::ostream
) for file-IO, unless you have a very good reason not to. Then you would most likely not have this problem, as you could just write this:
std::ifstream file("myfile.txt");
while(file) { // this checks for any error
// do stuff with file
}
Possibly the compiler is optimizing your code. Assigning out_file to NULL a moment before it gets assigned to by the return value of fopen is pointless. The compiler might know that and not bother assigning NULL.
You could define and initialize the value on one line:
FILE* out_file = fopen( filename, "w" )
But, this won't make your problem go away. As somebody commented, you might be looking at the wrong bit of code as there doesn't appear to be much wrong with this.
You could try creating a minimal app that does just the operation you want and see if that works ok before reintroducing the rest of the code.
精彩评论