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