output to file using ofstream without closing stream.

Posted on 2003-02-28
Medium Priority
Last Modified: 2007-12-19
Hi everyone,

Apologies for the newbie question, but I'm having problems with the ofstream object.

Basically I'm using it to create a log. I have a log object which when created opens the log file with the following command...

oFile.open ("ITSO Module.log", ofstream::out | ofstream::app, filebuf::sh_read);
oFile << "----------------------------------------------------------------\n";

I then call a writeLog(LPSTR lpMessage) command which outputs to the log like so....

oFile << lpMessage;

When the program exits the log object is destroyed and the following call made...


The problem is that I can't see tha changes made to the log file until I close the app. How can I make it so the updates to the log file can be seen as they happen (i.e. without having to close the file)

Thanks in advance,

Question by:Beechy
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 11

Accepted Solution

cup earned 100 total points
ID: 8040425
Try oFile.flush();

Assisted Solution

Hoegje earned 100 total points
ID: 8040438
Maybe this would work, it does on the output stream (cout) but I haven't tested it on the filestream. But I guess this should work :

oFile << "----------------------------------------------------------------\n" << flush;

This flushes the output stream buffer so it writes it immediatly. Otherwise your output will be buffered to have the least needed file accesses. This is because accessing files is rather slow.

But I guess endl does the same job for you, on some comilers it does !

So instead of \n use std::endl ==>

oFile << "----------------------------------------------------------------" << endl;

endl writes a newline and flushes the buffer, so this should do it as well.

Good luck.
LVL 11

Expert Comment

ID: 8043999
Hoegie - you're right there.  I'd forgotten that endl does a flush.
LVL 11

Expert Comment

ID: 9847716
No comment has been added lately, so it's time to clean up this TA. I will
leave a recommendation in the Cleanup topic area that this question is:

Answered: Points split between cup and Hoegje

Please leave any comments here within the next seven days.

Experts: Silence means you don't care. Grading recommendations are made in light
of the posted grading guidlines (http://www.experts-exchange.com/help.jsp#hi73).


-bcl (bcladd)
EE Cleanup Volunteer

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question