This is a C++ question. This function is supposed to read only the last line of
ID: 3765196 • Letter: T
Question
This is a C++ question. This function is supposed to read only the last line of the text file. Any help would be appreciated.
Here is what is in the text file: (the file is named punchline.txt)
asfasdfasdfasdfsdf
asdfasdfsadfsadfsadf
asdfsadfsdfsdf
"I can't work in the dark," he said.
I can not seem to get this to work. The function should display only the last line of the file it is passed.
IT should find this line by seeking to the end of the file and then backing up to the beginning of the last line.
void displayLastLine(ifstream &infile)
{
cout << " Punchline: ";
string line;
infile.seekg(0L, ios::end);
if (infile)
{
getline(infile, line);
while (line < 1)
{
long pos = infile.tellg();
seekTo--;
}
infile >> line;
cout << " " << line << endl;
}
}
Explanation / Answer
#include<iostream>
#include<string>
using namespace std;
void displayLastLine(ifstream &infile)
{
infile.open("Myfile");
cout << " Punchline: ";
string line;
if (infile)
{
while(!infile.eof())
{
getline(infile, line);
infile >> line;
}
cout << " " << line << endl;
}
}
void main()
{
ifstream f;
displayLastLine(f);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.