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

原文链接: https://www.cnblogs.com/godcity/p/5942393.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/241778

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 下午9:53
下一篇 2023年2月13日 下午9:53

相关推荐