read file line by 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))
{
cout << line << endl;
}
}
Example 2: 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();
}
}