小编Par*_*mbo的帖子

如何使用 fstream (C++) 从文件中读取特定行

我是一名 n00b C++ 程序员,我想知道如何从文本文件中读取特定行。例如,如果我有一个包含以下几行的文本文件:

1) Hello
2) HELLO
3) hEllO
Run Code Online (Sandbox Code Playgroud)

我将如何阅读,比如说第 2 行并将其打印在屏幕上?这是我到目前为止..

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{
    string sLine = "";
    ifstream read;

    read.open("input.txt");

    // Stuck here
    while(!read.eof()) {
         getline(read,1);
         cout << sLine;
    }
    // End stuck

    read.close();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

评论部分之间的代码是我被卡住的地方。谢谢!!

c++

1
推荐指数
1
解决办法
1万
查看次数

标签 统计

c++ ×1