docs:programming:cpp:read_a_file_by_line

read a file by line

#include <iostream>
#include <string>
#include <fstream>
 
using namespace std;
 
bool processFile(const char *filename){
	ifstream reader(filename);
 
	if( !reader )
		return false;
 
	string line;
	while( true ){
		getline(reader,line);
		if( !reader.eof() ){
			cout << line << endl;
		}else{
			break;
		}
	}
 
	reader.close();
 
	return  true;
}
 
int main(int argc, const char **argv){
 
	processFile(argv[1]);
 
	return 0;
}
c++ -o readfile readfile.cpp
  • docs/programming/cpp/read_a_file_by_line.txt
  • Last modified: 2009/02/09 22:27
  • by billh