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;
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM