Connecting Tech Pros Worldwide Help | Site Map

Help on ofstream ?

tvn007@hotmail.com
Guest
 
Posts: n/a
#1: Nov 9 '05
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);

John Harrison
Guest
 
Posts: n/a
#2: Nov 9 '05

re: Help on ofstream ?


tvn007@hotmail.com wrote:[color=blue]
> 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);
>[/color]

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
Closed Thread