I am writing a C++ program which opens a data file and then displays its content
ID: 3552880 • Letter: I
Question
I am 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 now I want to be able to just display a specific line using a printline() function. Any help would be greatly appreciated. Here's the code that I have written so far:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
string filename1;
ifstream qFile1;
string qLine1;
cout<<"Please enter your file name (robotics.txt): "<<endl;
cin>>filename1;
qFile1.open(filename1.c_str());
if (!qFile1.good())
{
cout<<"Could not open file"<<endl;
return 1;
}
while (!qFile1.eof())
{
getline(qFile1, qLine1);
cout<<qLine1<<endl;
}
qFile1.close();
return 0;
}
Explanation / Answer
void printline(int linenumber)
{
string qLine1;
for(int i=0;i<linenumber;i++)
{
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.