【问题标题】:endl not creating new line in output fileendl 不在输出文件中创建新行
【发布时间】:2017-08-21 00:13:42
【问题描述】:

这是我现在的代码

#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char* argv[])
{

// set up input file
ifstream lInput;    // declare an input file variable (object)
ofstream lOutput;
lInput.open(argv[1], ifstream::binary); // open an input file (binary)
lOutput.open(argv[2], ofstream::binary);
if (!lInput.good())
{
    // operation failed
    cerr << "Cannot open input file " << argv[1] << endl;
    return 2;       // program failed (input)
}

lOutput << "test" << endl;
lOutput << "test2" << endl;

我目前的输出是

测试测试2

我是怎么做到的

测试

测试2

感谢您的帮助

编辑:测试到“测试”和测试2到“测试2” 编辑2:lOutpt 到 lOutput

【问题讨论】:

  • 最好发布您使用的实际代码。这样一来,您就可以删除您现在质疑的未定义内容太多的拼写错误。
  • 如何查看输出文件?您可以将 lOutput.open(argv[2], ofstream::binary); 更改为 lOutput.open(argv[2]); 以查看是否有所不同。
  • 相关:stackoverflow.com/a/8281481/4926357。不要将&lt;&lt; 与二进制流一起使用。

标签: c++ fileoutputstream endl


【解决方案1】:

C 和 C++ 有两种类型的文件:textbinary。二进制文件不是文本。他们没有行,所以他们没有行尾。如果您想明智地谈论行尾和其他与文本相关的事情,请使用 text 文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-08
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多