Help on ofstream ?

Discussion in 'C++' started by tvn007@hotmail.com, Nov 9, 2005.

  1. Guest

    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);
    , Nov 9, 2005
    #1
    1. Advertising

  2. wrote:
    > 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
    John Harrison, Nov 9, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    609
  2. Tom Johnson
    Replies:
    4
    Views:
    385
    red floyd
    Aug 15, 2003
  3. ern

    ofstream help needed

    ern, Feb 2, 2006, in forum: C Programming
    Replies:
    3
    Views:
    271
    Michael Mair
    Feb 2, 2006
  4. Replies:
    0
    Views:
    319
  5. Squid Seven

    ofstream * vs. ofstream

    Squid Seven, Jul 13, 2005, in forum: C++
    Replies:
    5
    Views:
    595
    Ivan Johansen
    Jul 14, 2005
Loading...

Share This Page