user9245335
user9245335

Reputation:

Writing to file in real time before closing it

I write my output results which are emitted by Solver() function (a function in caffe third library), in a file with these command:

if(std::freopen("redir.txt", "w", stdout)) {
    std::printf("stdout is redirected to a file\n"); // this is written to redir.txt
    solver->Solve();
    std::fclose(stdout);
}

but since the Solve() function emits outputs continuously, but the redir.txt will not be updated until the ‍std::fclose(stdout); is executed. So I can't see the results real time.

How can I update my file in real time?

Upvotes: 0

Views: 541

Answers (2)

user9218823
user9218823

Reputation:

Commonly std::flush maybe works.

Upvotes: -2

Jesper Juhl
Jesper Juhl

Reputation: 31467

Use std::flush at regular intervals to flush the written (buffered) data to the file.

Don't flush too often though or performance will be impacted.

Upvotes: 5

Related Questions