문제

As far as I know the flag app seeks to the end before each write const ios_base::openmode std::ios_base::app [static] Seek to end before each write.

the following program output is: recostream789

std::string str("t2: 123456789");
std::ostringstream ostr(str,std::ios_base::out|std::ios_base::app);
ostr << "recostream";
std::cout << ostr.str() << std::endl;

shouldn't it output: t2: 123456789recostream instead?

I'm using vs2010

Compiled code image

도움이 되었습니까?

해결책

This is open defect LWG #2121, opened by Josuttis earlier this year. His report uses GCC and Visual Studio as the examples as well, to quote:

Note the following program:

string s("s1: 123456789");
ostringstream s1(s, ios_base::out|ios_base::app);
s1 << "hello";
cout << s1.str() << endl;

With g++4.x it prints:

s1: 123456789hello

With VisualC++10 it prints:

hello23456789

Mote that the behavior of the flag ios_base::ate was not explicitly specified for stringstreams in C++03, but C++11 added detailed stringstream-specific effects for it. It didn't add such details for ios_base::app, so some compilers didn't bother implementing it.

다른 팁

Yes, I think it should -- at least to me this looks like a compiler (or, technically, library) bug.

Doing a quick check, g++ (4.7.1) seems to agree -- it produces t2: 123456789recostream as expected.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top