CPP/IO/ReadingLines

From ProgrammingExamples
< CPP
Revision as of 15:12, 23 June 2010 by Galik (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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;
}