I want to read recent added lines in a file.txt with c++ . Everytime I add some new lines in the file I want to read just those recent lines.
This code below just read all the lines of the file
void Reading::read(const char* path)
{
string str_path = string(path);
string str;
ifstream file1(path);
if(str_path.substr(str_path.find_last_of(".")+ 1)== "jrn")
{
if(file1.is_open())
{
while (getline(file1, str))
{
cout << str << "\n";
}
file1.close();
}
else
cout <<"File not found " << endl;
file1.close();
}
else
{
cout << "Can't read this type of file" << endl;
}
}
Please can you help me to solve the problem . Thank you .
Have your Reading
class cache the return value from file1.tellg()
, call file1.seekg()
to go to the last read location before reading the lines.
https://www.cplusplus.com/reference/istream/istream/tellg/
https://www.cplusplus.com/reference/istream/istream/seekg/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.