开发者

Reading text file line by line in c++ in UVA

开发者 https://www.devze.com 2023-03-14 10:55 出处:网络
Please help me in reading the text file efficiently in c++ according to the UVA (ACM programming) standards.

Please help me in reading the text file efficiently in c++ according to the UVA (ACM programming) standards.

开发者_Python百科

If you can provide some code snippet then i will be very thankful.


If you want to read a text file par line.

#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++

This can be written also as following.

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

Or if you want to read whole of one.

ifstream ifs("data.txt");
string str((istreambuf_iterator<char>(ifs)), istreambuf_iterator<char>());
// language: c++
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号