2

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

Vinícius
  • 15,498
  • 3
  • 29
  • 53

2 Answers2

4

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.

Community
  • 1
  • 1
Cubbi
  • 46,567
  • 13
  • 103
  • 169
2

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.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 1
    Note [this bug report](http://connect.microsoft.com/VisualStudio/feedback/details/692248) which claims microsoft is correct. – Jesse Good Oct 18 '12 at 02:21
  • @JesseGood: I'll send STL an email -- I see nothing there that says that §27.5.3.1.4/1: `app seek to end before each write` should be ignored. – Jerry Coffin Oct 18 '12 at 02:44
  • @JerryCoffin: What about `if mode & ios_base::ate is true, pptr() == pbase() + s.size() holds, otherwise pptr() == pbase() is true`, the `otherwise pptr() == pbase()` part seems to say the current position should be pointing at the beginning **unless** `ios_base::ate` is used. – Jesse Good Oct 18 '12 at 02:52
  • @JesseGood: I had written another comment in reply, but looking at Cubbi's answer, it appears it's already been reported as a bug to the committee. I still think gcc has it right and VC++ has it wrong though. With `ios_base::app` specified, the put pointer should be positioned to the beginning at construction, but to the end immediately before a write. I see no real conflict or lack of clarity here. – Jerry Coffin Oct 18 '12 at 03:18
  • Yes, intuitively, gcc has it right although the current wording in the standard is insufficient (and it seems P.J. Plauger is against it although I didn't understand the reasoning). – Jesse Good Oct 18 '12 at 03:41