C++逐行读取文本文件
本文将使用C++实现逐行读取文本文件并显示;示例代码如下:
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[]) {
if (argc != 2) {
cout << "please enter a filename." << endl;
exit(0);
}
ifstream infile(argv[1], ios::in);
string line;
while (getline(infile, line)) {
cout << line << endl;
}
infile.close();
}
编译
gcc main.cpp -lstdc++