C++-文件【1】-按行读文本文件的两种方法

测试环境——

系统:Win7 64bit

编译器:TDM-GCC 4.9.2 64-bit Release

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

int main() {

    //方式一
    std::ifstream ifile("file.txt");
    int a, b;
    while (ifile >> a >> b) {
        std::cout << a << "	" << b << std::endl;
    }
    ifile.close();

    std::cout<<"--------------------------------"<<std::endl;
    //方式二
    ifile.open("file.txt");
    std::string line;
    while (std::getline(ifile, line)) {
        std::cout << line << std::endl;
    }
    ifile.close();

    return 0;
}
原文地址:https://www.cnblogs.com/godcity/p/5942393.html