c++ - 如何使 cout 表现为二进制模式?

标签 c++ iostream cout

每次我执行 'cout << endl' 甚至 'cout << "\n"' 然后在 Windows 下启动我的程序以输出到文件 ("a.exe < test.in > result.out") 我在“result.out”中得到“\r\n”行结尾。

到底有没有办法阻止它这样做,只在每个 'cout << "\n"' 上输出 "\n"?

提前致谢。

最佳答案

这适用于 Visual Studio 2013:

#include <io.h>
#include <fcntl.h>
#include <iostream>

int main( int argc, char * argv[] )
{
    _setmode( _fileno( stdout ),  _O_BINARY );
    std::cout << std::endl;
}

它只会输出[0A],而不是[0D][0A]。

关于c++ - 如何使 cout 表现为二进制模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5654067/

相关文章:

c++ - C/C++ 打包和压缩

C++:继承 std::basic_streambuf 的问题

c++ - 从控制台获取字符串但不知道长度

c++ - 什么是 CString::StringTraits?它是做什么用的?好像没有文档

python - 无法将长整数从嵌入式 Python 返回到 C++

c++ - 我如何在 wxWidgets 中重定向标准输入(istream)?

c++ - 正确重载 stringbuf 以替换 MATLAB mex 文件中的 cout

c++ - std::cout 将 uint8_t 作为字符处理

c++ - 错误: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'List' )

c++ - 将 makefile 转换为 cmake