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 << "......." ;

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()).



@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

25 Answered Questions

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

15 Answered Questions

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

10 Answered Questions

1 Answered Questions

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

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

28 Answered Questions

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

8 Answered Questions

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

38 Answered Questions

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

40 Answered Questions

19 Answered Questions

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

  • 2008-12-17 20:29:24
  • Gabriel Isenberg
  • 528979 View
  • 1833 Score
  • 19 Answer
  • Tags:   c++ linux profiling

13 Answered Questions

[SOLVED] How to flush output of print function?

Sponsored Content