Help on ofstream ?

T

tvn007

could someone please help me to point out what wrong with the code
below ?

#define OUPUT_DIR "/home/user/OUT/test.out"
if (appenndmode=="YES"){
ofstream out(OUTPUT_DIR,ios::app); //error: 'out' undeclared
}
//////////////////////////////////////////////////////////
However, it works if I do this:
#define OUPUT_DIR "/home/user/OUT/test.out"

ofstream out(OUTPUT_DIR,ios::app);
 
J

John Harrison

could someone please help me to point out what wrong with the code
below ?

#define OUPUT_DIR "/home/user/OUT/test.out"
if (appenndmode=="YES"){
ofstream out(OUTPUT_DIR,ios::app); //error: 'out' undeclared
}
//////////////////////////////////////////////////////////
However, it works if I do this:
#define OUPUT_DIR "/home/user/OUT/test.out"

ofstream out(OUTPUT_DIR,ios::app);

If you put a variable declaration inside an if statement then it only
exists inside that if statement. So when you try to use it outside the
if statement you get an undeclared error.

You probably want something like this

ofstream out;
if (appenndmode=="YES"){
out.open(OUTPUT_DIR,ios::app);
}

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top