By anon


2010-02-26 11:25:01 8 Comments

I'm on MacOSX.

In the logger part of my application, I'm dumping data to a file.

suppose I have a globally declared std::ofstream outFile("log");

and in my logging code I have:

outFile << "......." ;
outFile.flush();

Now, suppose my code crashes after the flush() happens; Is the stuff written to outFile before the flush() guaranteed to be written to disk (note that I don't call a close()).

Thanks!

4 comments

@Danilo Piazzalunga 2010-02-26 13:08:58

As an alternative approach, you can disable buffering altogether with

outFile.rdbuf()->pubsetbuf(0, 0);

Writing to an unbuffered fstream may hurt performance, but worrying about that before measuring would be premature optimization.

@Drew Hall 2010-02-26 11:50:17

As long as flush() has returned, your program has successfully put the output in the OS's hands. Unless the OS (or disk) crashes, your data should be on disk next time the disk writes (note that the disk likely has a solid state cache of its own).

Until flush() returns, it's anybody's guess how much will make it to the disk.

@anon 2010-02-26 11:30:37

flush() flushes the iostream library's buffers - however the data is almost certainly not immediately flushed from the operating system's buffers at exactly the same time, so there is a small period during in which an operating system crash could lose you data. You can of course lose data at any time if you suffer a hard disk failure, whether the data was written or not, so I wouldn't worry too much about this.

@Timo Geusch 2010-02-26 11:28:38

From the C++ runtime's point of view, it should have been written to disk. From an OS perspective it might still linger in a buffer, but that's only going to be an issue if your whole machine crashes.

Related Questions

Sponsored Content

23 Answered Questions

[SOLVED] What is the "-->" operator in C++?

27 Answered Questions

[SOLVED] Easiest way to convert int to string in C++

37 Answered Questions

13 Answered Questions

[SOLVED] How to flush output of print function?

16 Answered Questions

[SOLVED] How can I profile C++ code running on Linux?

  • 2008-12-17 20:29:24
  • Gabriel Isenberg
  • 493375 View
  • 1736 Score
  • 16 Answer
  • Tags:   c++ unix profiling

13 Answered Questions

[SOLVED] What is the effect of extern "C" in C++?

36 Answered Questions

[SOLVED] Why is "using namespace std;" considered bad practice?

1 Answered Questions

[SOLVED] The Definitive C++ Book Guide and List

  • 2008-12-23 05:23:56
  • grepsedawk
  • 2247520 View
  • 4246 Score
  • 1 Answer
  • Tags:   c++ c++-faq

10 Answered Questions

8 Answered Questions

[SOLVED] Read file line by line using ifstream in C++

  • 2011-10-23 20:24:31
  • dukevin
  • 1265727 View
  • 582 Score
  • 8 Answer
  • Tags:   c++ file-io ofstream

Sponsored Content