Passing ofstream object to another function

This is a discussion on Passing ofstream object to another function within the C++ Programming forums, part of the General Programming Boards category; Hi, need some help here. i have an idea of - Code: int main() { ofstream ofs; } void openFile(ofstream ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    2

    Passing ofstream object to another function

    Hi, need some help here.

    i have an idea of -

    Code:
    int main()
    {
      ofstream ofs;
    }
    
    void openFile(ofstream var_ofs)
    {
      var_ofs.open(arg);
    }
    
    void writeFile(ofstream var_ofs)
    {
      var_ofs.write(arg);
    }
    i am trying to make it into functions so that i can have multiple threads writing to the file using the same stream. i understand that declaring the ofstream variable as global will be one solution. however, due to requirements, that cannot be done.

    how can this be done without using global variables? i have tried passing the ofstream as pointer but that wouldn't work too.
    Last edited by Dave_Sinkula; 07-24-2006 at 09:32 PM. Reason: Added [code][/code] tags -- learn to use them yourself.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,514
    Make the parameters references to an ofstream.
    The programmers wife tells him: Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    2
    it works. thankyou

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21