-2


请帮助我根据 UVA(ACM 编程)标准在 C++ 中有效地阅读文本文件。

如果您可以提供一些代码片段,那么我将非常感激。

4

1 回答 1

0

如果要读取文本文件 par 行。

#include <fstream>
#include <iostream>

using namespace std;

int main() {
    ifstream ifs("data.txt");
    string buf;

    while(ifs && getline(ifs, buf)) {
        cout << buf << endl;
    }
    return 0;
}
// language: c++

这也可以写成如下。

ifs >> buf;
cout << buf << endl;
// language: c++

或者,如果您想阅读全文。

ifstream ifs("data.txt");
string str((istreambuf_iterator<char>(ifs)), istreambuf_iterator<char>());
// language: c++
于 2011-06-21T01:48:42.427 回答