ofstream issue.

This is a discussion on ofstream issue. within the C++ Programming forums, part of the General Programming Boards category; I created an a variable for my info logger Code: std::ofstream FileLog; and in the constructor of my logger I ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    97

    ofstream issue.

    I created an a variable for my info logger
    Code:
    std::ofstream FileLog;
    and in the constructor of my logger I do
    Code:
    FileLog.open("log.txt");
    And while I can add everything just fine, it doesn't actually outputs the content of FileLog until I do the following in the logger destructor.
    Code:
    FileLog.close();
    How can I make sure that it writes to the log.txt file every time I add something to the log? I want to be able to look at the log's content even if my application crashes.

    Thanks in advance.

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Output streams must be flushed for their contents to be written to the output device.

    look at std::endl it isn't just for cout'ing .

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,789
    ...or after each write op you can do:
    Code:
    FileLog.flush();
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    Oh, that was it. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  2. ofstream and FILE behaviour
    By MrLucky in forum C++ Programming
    Replies: 7
    Last Post: 06-21-2007, 05:45 PM
  3. Capturing key presses
    By cgod in forum Windows Programming
    Replies: 6
    Last Post: 11-25-2004, 12:10 PM
  4. Using ofstream in private of a class-errors
    By loobian in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2003, 09:06 PM
  5. ofstream and ifstream for searching and writing
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-17-2003, 07:34 AM

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