CPP/IO/ReadingLines

From ProgrammingExamples
< CPP
Jump to: navigation, search

ReadLines.cpp

#include <string>
#include <fstream>
#include <iostream>
 
int main()
{
	// Open file for input
	std::ifstream ifs("input.txt");
 
	std::string line; // string to contain each line
 
	// By placing the getline() function
	// inside the while condition you
	// guarantee that inside the loop
	// the read was successful and that line is valid.
	while(std::getline(ifs, line))
	{
		// deal with each line here...
		std::cout << line << std::endl;
	}
 
	return 0;
}