I've got a dump file from a mathematical problem that records all tried solutions to a specific formula. Since I will be starting and stopping my program multiple times, but I do not want to lose the tried solutions, I need to access the most recently tried solution (the one at the bottom of the list), but I'm not sure how to do that.
I really don't want to read the entire file, as after a while it may grow to be several gigabytes and that'd be awkward.
getline() and seekg() seem like plausible options, but I can't figure out how to combine them into something usable. Is there anyone who could help me?
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int main(){
int startVal[1160];
char startValChar[1160];
ifstream valStream;
valStream.open("solnsdump.txt");
if (valStream.fail()){
cout << "No solution dump file found. Starting from 1160x0. \n";
exit(1);
}
bool isFound = false;
while (!valStream.eof()){
valStream.seekg(valStream.end - 1161);
};
}
This is what I have so far, but I don't think it'll work. Any tips would be appreciated.
#include <iostream>
#include <fstream>
#include <string>
int main()
{
const std::streamoff max_line_size_allowance = 2000 ; // adjust as required
std::string path = "/usr/include/unistd.h" ;
std::ifstream file( path, std::ios::ate ) ; // open for input, seek to end
if( !file.seekg( -max_line_size_allowance, std::ios::cur ) ) // back by max_line_size_allowance characters
{
// this file has less than max_line_size_allowance characters
file.clear() ; // clear the error state
file.seekg(0) ; // and seek to the beginning
}
std::string line, last_line ;
while( std::getline( file, line ) ) last_line = line ;
std::cout << last_line << '\n' ;
}