I am using the new C++23 std::print
function from the <print>
utility.
My understanding is that the C++ standard does not define whether or not std::print
and std::println
should flush the stream after writing to it.
If I wanted to flush the stream (stdout
or stderr
), what is the most appropriate solution in C++ 23?
I am using the new C++23 std::print
function from the <print>
utility.
My understanding is that the C++ standard does not define whether or not std::print
and std::println
should flush the stream after writing to it.
If I wanted to flush the stream (stdout
or stderr
), what is the most appropriate solution in C++ 23?
2 Answers
Reset to default 0Use one of
std::flush
which flushes a std::basic_ostream
or
std::fflush
which operates on a FILE*
.
Detailed references can be found below
- https://en.cppreference.com/w/cpp/io/manip/flush
- https://en.cppreference.com/w/cpp/io/c/fflush
#include <cstdio>
#include <iostream>
#include <print>
int main()
{
std::println(stdout, "One");
std::fflush(stdout);
std::println(std::clog, "Two");
std::flush(std::clog);
}
std::fflush(stdout);
– Ted Lyngmo Commented Jan 26 at 22:22std::print
/std::println
should be flushed in C++23. That's why I explicitly asked what is the most appropriate solution in C++ 23? Because it was not clear to me and although there was a solution embedded inside an answer to my previous question, this was a different question... – user2138149 Commented Jan 27 at 13:10std::flush
andstd::fflush
. And if anything, they explain in more detail the difference between them. – HolyBlackCat Commented Jan 27 at 14:09