【问题标题】:Opening a file; but skipping each line打开文件;但跳过每一行
【发布时间】:2014-06-09 20:30:30
【问题描述】:

我想知道在使用fstream 打开文件时如何跳过行。当我打开文件时,它会返回所有聚集在一起的数字,例如“201051535402530”

这是我的代码。

#include <iostream>
#include <vector>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    string testing;
    ifstream myfile;
    myfile.open ("inputfile");
    while (!myfile.eof())
    {
        getline(myfile, testing);
        cout << testing;
    }
    return 0;
}

输入文件被列为:

20
10
5
15
35
40
25
30

【问题讨论】:

  • cout &lt;&lt; testing; 更改为 cout &lt;&lt; testing &lt;&lt; " ";
  • cout &lt;&lt; testing &lt;&lt; "\n"; 在单独的行上打印每个项目。
  • 还有,谁教while (!eof)?错了。

标签: c++ fstream


【解决方案1】:

getline() 读取一行时,它会丢弃尾随的换行符。如果你想在你的输出中打印一个,你需要自己把它放在那里:

cout << testing << endl;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    相关资源
    最近更新 更多