Difference between revisions of "CPP/IO/ReadingLines"

From ProgrammingExamples
< CPP
Jump to: navigation, search
(Created page with '==ReadLines.cpp== <source lang="cpp"> #include <string> #include <fstream> #include <iostream> int main() { // Open file for input std::ifstream ifs("input.txt"); std::strin…')
 
(No difference)

Latest revision as of 16:12, 23 June 2010

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;
}