c++ read line 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))
{
cout << line << endl;
}
}
Example 2: getline cpp
#include <iostream>
#include <string>
int main()
{
string namePerson{};
getline(cin, namePerson);
std::cout << namePerson;
}
Example 3: get line C++
#include <iostream>
#include <string>
int main ()
{
std::string name;
std::cout << "Please, enter your full name: ";
std::getline (std::cin,name);
std::cout << "Hello, " << name << "!\n";
return 0;
}
Example 4: c++ read file line by line
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
fstream newfile;
newfile.open("tpoint.txt",ios::out);
if(newfile.is_open())
{
newfile<<"Tutorials point \n";
newfile.close();
}
newfile.open("tpoint.txt",ios::in);
if (newfile.is_open()){
string tp;
while(getline(newfile, tp)){
cout << tp << "\n";
}
newfile.close();
}
}