C++ Program to Read and Display a Text File Line by Line

Problem:

Write a c++ program to read and display the contents of a file line by line. Write a C++ program to read a text (.txt) file line by line into string and display it.

C++ Program / Source Code:

This program is successfully compiled and run on a Linux system. The program output is also shown below.
Here is the source code of a program for reading a text file line by line in c++
/* Aim: Write a C++ program for reading a text file line by line and then displaying it.*/

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
	ifstream fin;
	char line[100], fname[20];

	cout<<"\n Enter file name for reading it line by line:- ";
	cin>>fname;
	cout<<endl;

	fin.open(fname);

	if(!fin)
	{
	cout<<"\n Error in opening file ! \n";
	return 0;
	}

	while(fin)
	{
	fin.getline(line,100);
	cout<<line<<endl;
	}

	fin.close();

	return 0;

}

/* Output of above code / Runtime test cases:- 

 Enter file name for reading it line by line:- msg.txt

 Hey, you are learning about reading a text file line by line in C++. 
 Take a look at other articles and tutorials. 
 Fresh programs and articles are published here daily.
 Use the search bar to search your problem.

*/