Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

output to file using ofstream without closing stream.

Posted on 2003-02-28
4
Medium Priority
?
247 Views
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...

oFile.close()

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,

Beechy
0
Comment
Question by:Beechy
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
cup earned 100 total points
ID: 8040425
Try oFile.flush();
0
 
LVL 1

Assisted Solution

by:Hoegje
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.
0
 
LVL 11

Expert Comment

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

Expert Comment

by:bcladd
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).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

564 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