Note that there are some explanatory texts on larger screens.

plurals
  1. POConditionals using filehandles in C++
    primarykey
    data
    text
    <p>I'm working with a C++ library which makes extensive use of constructs like:</p> <pre><code>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( ... ) </code></pre> <p>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.</p> <p>I've tried adding</p> <pre><code>out_file = NULL </code></pre> <p>but this doesn't seem to help - in fact, according to the debugger, it doesn't change the value of out_file.</p> <p>Can anyone help as to:</p> <p>Is this a recognised and sensible way to do file IO (i.e. using the file pointers in conditionals)</p> <p>Why is the value of the pointer not being set to null?</p> <p>How can I set it to null?</p> <p>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.</p> <p><strong>EDIT:</strong> 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.</p> <pre><code>class IO { private: FILE* opFile IO() { //At this point, opFile == 0x40 opFile = NULL; //At this point opFile is still 0x40 } } </code></pre> <p>So obviously, if it comes out of the constructor with a non-null value, anything like:</p> <pre><code>if( opFile ) fprintf( ... ) </code></pre> <p>will fail. But how is it managing to come out of the constructor with a non-null value?</p> <p>And in case it helps, this works "as expected" under gcc on OSX, but not g++-4.3 or g++4.4 on Ubuntu.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload