Thread: closing ofstream issue

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    40

    closing ofstream issue

    I obviously need a little help learning the logistics of ofstream. In the following code I get the error shown at bottom.
    --------------------------------------------------
    Code:
      ofstream outf("DrawDown.txt"); 
      if (!outf.good())
      { cerr << "Problem opening DrawDown file" << endl; "\n"; }
    
      outf << "This went out " << iDebugPrtCt << "\n";
      
      if (outf.fail() )
      { cerr << "Failed write DrawDown file" << endl; "\n"; }
      
      outf.close();
    
      ofstream outf("DrawDown.txt",  ios_base::out | ios_base::app );
    -------------------------
    pulls this error on compile, redeclaration of 'std::ofstream outf'

  2. #2
    Guest
    Guest
    On line 1 you declare a std::ofstream object called outf. On line 12, you declare another object using the same name. Replacing ofstream with int, it's the equivalent of doing:
    Code:
    int n = 5;
    int n = 11; // n already exists, can't do that.
    Is there a specific thing you want to do with the call on line 12? Do you want to open a different file maybe? Or open the same file to read from it?
    Last edited by Guest; 05-28-2016 at 06:18 PM.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Are you sure you need lines 10 and 12 at all?
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    May 2016
    Posts
    40
    Quote Originally Posted by Guest View Post
    On line 1 you declare a std:fstream object called outf. On line 12, you declare another object using the same name. Replacing ofstream with int, it's the equivalent of doing:
    Code:
    int n = 5;
    int n = 11; // n already exists, can't do that.
    Is there a specific thing you want to do with the call on line 12? Do you want to open a different file maybe? Or open the same file to read from it?
    Sorry, I had a bit of trouble with my password app working with this forum or maybe it's FireFox ? Anyhow I was finally able to get back on.
    Ok, thank you both much, I see it now, obviously I was getting a bit preoccupied with the immediate. The below works thanks to you pointing me to a different angle.

    Code:
    ofstream outf("DrawDown.txt"); 
      if (!outf.good())
      { cerr << "Problem opening DrawDown file" << endl; "\n"; }
     
      outf << "This went out " << iDebugPrtCt << "\n";
       
      if (outf.fail() )
      { cerr << "Failed write DrawDown file" << endl; "\n"; }
       
      outf.close();
     
      /// Took out-> ofstream outf("DrawDown.txt",  ios_base::out | ios_base::app );
    
      /// And put in,
      outf.open("DrawDown.txt",  ios_base::out | ios_base::app );
      outf << "This went out " << iDbugPrtCt << "\n";
      outf.close();
    I have one other question if you have time, say I want to access the same file across different class functions and for loop scopes. Can I just declare the ofsteam obj globally to work around this. What implementations do you guys use or recommend for this?

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I have one other question if you have time, say I want to access the same file across different class functions and for loop scopes. Can I just declare the ofsteam obj globally to work around this. What implementations do you guys use or recommend for this?
    Well, globals have their place but it really feels like there is a general lack of scope and structure. It's also hard to know what you're really doing in order to offer good advice. As an alternative, though, consider overloading operator<< so you can just do this with some objects:
    Code:
    FooClass bar;
    // ... ...
    outf << bar << endl;
    That should cut down on how much you have to pass around a file handle to some place or object. You might find that with this glorified ToString() method you don't need a global object after all.

    These articles may be of some help if you need it. There is considerable overlap... I don't know how much detail you need or want about this.
    A Gentle Introduction to IO Streams in C++ - Cprogramming.com
    C++ Programming/Operators/Operator Overloading - Wikibooks, open books for an open world

    It might seem weird using bitwise shift operators for read and write operations, but that is what's convention.
    Last edited by whiteflags; 05-29-2016 at 04:34 PM.

  6. #6
    Registered User
    Join Date
    May 2016
    Posts
    40
    Quote Originally Posted by whiteflags View Post
    . . .< . . . . > though, consider overloading operator<< so you can just do this with some objects:
    Code:
    FooClass bar;
    // ... ...
    outf << bar << endl;
    . . . . .
    Wow, thanks that's definitely outside any box I was pondering, I really like that idea.

    And thanks for the links.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bandwidth issue / network issue with wireless device communication
    By vlrk in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-05-2010, 11:52 PM
  2. ofstream
    By Ducky in forum C++ Programming
    Replies: 54
    Last Post: 12-29-2007, 01:47 PM
  3. ofstream issue.
    By Hulag in forum C++ Programming
    Replies: 3
    Last Post: 07-18-2006, 06:09 AM
  4. Using ofstream
    By alvifarooq in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2004, 09:06 PM
  5. If/Ofstream
    By SirCrono6 in forum C++ Programming
    Replies: 3
    Last Post: 12-21-2003, 04:58 AM

Tags for this Thread