writing a C++ program which opens a data file and then displays its contents with line numbers. That is the program should display the number 1 and then the first line of the file, then the number 2 and the second line of the file, etc. I am able to open and display the file, but I can't seem to figure out how to read the file line by line. Any help would be greatly appreciated. Here's the code that I have written so far:
#include
#include
#include
using namespace std;
int main ()
{
string filename1;
ifstream qFile1;
string qLine1;
cout<<"Please enter your file name (robotics.txt): "<
cin>>filename1;
qFile1.open(filename1.c_str());
if (!qFile1.good())
{
cout<<"Could not open file"<
return 1;
}
while (!qFile1.eof())
{
getline(qFile1, qLine1);
cout<
}
qFile1.close();
return 0;
}