read line in c++ code example

Example 1: c++ read file line by line

#include <iostream>
#include <ifstream>
#include <string>

using namespace std;

ifstream file("file.txt");
if (file.is_open())
{
	string line;
	while (getline(file, line))
    {
    	// note that the newline character is not included
        // in the getline() function
    	cout << line << endl;
    }
}

Example 2: how to read a line from the console in c++

#include <string>
std::string str;
std::getline(std::cin, str);
// The output of std::getline(std::cin, str) will be stored in str.