打开文件;但是要跳过每一行

Opening a file; but skipping each line

本文关键字:一行 文件      更新时间:2023-10-16

我想知道如何在使用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

getline()读取一行时,它丢弃末尾的换行符。如果您希望在输出中打印一个,则需要自己将其放在那里:

cout << testing << endl;