ofstream doesn't flush
Asked Answered
T

5

13

I have the following code, running on Suse 10.1 / G++ 4.1.0, and it doesn't write to the file:

#include <fstream>
#include <iostream>

int main(){
    std::ofstream file("file.out");
    file << "Hello world";
}

The file is correctly created and opened, but is empty. If I change the code to:

#include <fstream>
#include <iostream>

int main(){
    std::ofstream file("file.out");
    file << "Hello world\n";
}

(add a \n to the text), it works. I also tried flushing the ofstream, but it didn't work.

Any suggestions?

Tiedeman answered 24/6, 2010 at 19:22 Comment(3)
How do you check the resulting file ?Bedaub
The program on Debian/Sid with g++ 4.4.4 works as expected and file.out contains the string (without newline). Try updating your compiler or OS - both are outdated.Stoat
How are you checking if the file is empty? Do this on the resulting file: wc <file> and see what the output is. It should be 0 2 11. 0 lines, 2 words, 11 characters. Without the newline, the output "Hello world" will run into your prompt, so you might just be missing it.Hutcherson
B
11

If you check your file doing a cat , it may be your shell that is wrongly configured and does not print the line if there is no end of line.
std::endl adds a \n and flush.

Bedaub answered 24/6, 2010 at 19:27 Comment(2)
Yeah, try doing cat file ; echo. Don't mindlessly use endl though. I hate code that uses that stupid construct and I wish the standards people would remove it from the standard. Flushing is expensive, and should always be done consciously and with forethought.Mithras
The problem was the buggy editor used in my work that doesn't show the line if there is no '\n' at the end. Obviously, vi shows it right. Thanks.Tiedeman
W
6

Don't know if this is what you tried but you should do:

file << "Hello World" << std::flush;

Update; I'm leaving this answer here because of the useful comments

Based on feedback, I'll modify my advice: you shouldn't have to explicitly call std::flush (or file.close() for that matter), because the destructor does it for you.

Additionally, calling flush explicitly forces an I/O operation that may not be the most optimized way. Deferring to the underlying iostreams and operating system would be better.

Obviously the OP's issue was not related to calling or not calling std::flush, and was probably due to attempting to read the file before the file stream destructor was called.

Weirdo answered 24/6, 2010 at 19:24 Comment(8)
The ofstream destructor should automatically flush.Mithras
@Omnifarious: That deserves a downvote? Is my answer wrong?Weirdo
@John Weldon - It is wrong because it doesn't really address the OPs actual problem. It might be useful in debugging and figuring out what the real problem is (and should therefor be a comment), but isn't a solution in itself. If it is meant as a solution it suggests a cargo-cult style solution in which one inserts code without understanding why it needs to be there.Mithras
Of course, nobody else really suggests a solution either. All the answers are basically debugging suggestions. But most of them are at least phrased as debugging suggestions. I wouldn't have downvoted if it hadn't had an upvote or if it had been phrased as a debugging suggestion instead of as a solution.Mithras
@Omnifarious; wouldn't you agree that you should flush the stream before closing it, rather than relying on the destructor?Weirdo
@John Weldon, no I wouldn't agree that you should do that. The flushing is an implementation detail. The conceptual model of the file is that you are writing to it with the << operator. It is ofstream's responsibility to maintain that model in all reasonable contexts. I should only have to reach in and fiddle with the implementation detail in specific cases where I need the actual file and the model presented by ofstream to match at a particular instant of time.Mithras
@Omnifarious; I'll defer to you, I think your case is compelling.Weirdo
It is wrong because it suggests the destructor of ofstream doesn't flush, when in fact it does.Retired
F
5

The destructor should flush and close the file.

I am pretty sure, the error is an another place, either

1) You do not check at the right point in time. At which point do you compare the content of the file, "after" the exits, or do you set a breakpoint before the program exits and then you check the files content?

2) Somehow the program crashes before it exits?

Flatting answered 24/6, 2010 at 19:55 Comment(1)
Yeah, if the program as given runs and after it's completely finished and you get your prompt back it doesn't contain the data, there's a bug somewhere.Mithras
E
0

Does

file << "Hello world" << std::endl;

work?

endl inserts a newline and flushes the buffer. Is that what you were referring to when you said that you'd already tried flushing it?

Empiric answered 24/6, 2010 at 19:24 Comment(0)
U
0

You are working on Linux, which is a POSIX-compliant system. The POSIX standard defines what a line is:

A sequence of zero or more non-newline characters plus a terminating newline character.

So without the newline character, the file contains 0 lines and is therefore empty.

Unready answered 16/5, 2018 at 13:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.