1. 程式人生 > >c++/逐行讀取txt檔案

c++/逐行讀取txt檔案

#include <fstream>   #include <string>   #include <iostream>   using namespace std;

int main() {     ifstream in("1.txt");     ///string filename;     string line;

    if (in) // 有該檔案       {         while (getline(in, line)) // line中不包括每行的換行符           {             cout << line << endl;         }     }     else // 沒有該檔案       {         cout << "no such file" << endl;     }

    return 0; }