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++

如需转载,请注明出处;本文地址:https://www.perfcode.com/p/cpp-read-text-file-line-by-line.html