c++ - 标志 ios_base::app 的错误行为

标签 c++ sstream

据我所知,app 标志在每次写入前都会寻找结束 const ios_base::openmode std::ios_base::app [static] Seek to end before each write.

下面的程序输出是: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;

它不应该输出:t2: 123456789recostream 吗?

我用的是vs2010

Compiled code image

最佳答案

这是今年早些时候由 Josuttis 打开的开放缺陷 LWG #2121 。他的报告也以 GCC 和 Visual Studio 为例,引用:

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

请注意,标志 ios_base::ate 的行为并未在 C++03 中明确指定用于字符串流,但 C++11 added 详细说明了它的字符串流特定效果。它没有为 ios_base::app 添加这样的细节,所以一些编译器没有费心去实现它。

关于c++ - 标志 ios_base::app 的错误行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12945933/

相关文章:

c++ - 我该如何实现这段代码?

c++ - 删除字符指针得到堆错误

c++ - `std::stringstream::fail()` 读后写应该返回什么值? (海湾合作委员会与 clang )

c++ - sstream 重新声明公共(public)访问编译器错误

c++ - 使用 sstream 将 std::string 转换为 int

c++ - 使用函数指针将字符串数组作为参数传递

c++ - 设置处理器与将在 Linux 上运行的 C++ 的关联

c++ - 数组数组的方法