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 << "\t" << 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;
}

 

posted @ 2016-10-09 15:23  上帝之城  阅读(9615)  评论(0编辑  收藏  举报