I read that page...I know how to position to the end of the file. I was hoping someone knew of a way to move backwards in the file by more than a character-by-character basis.
void openFile (ifstream& f)
{
constlong LINE_LEN = 23;
int pos;
// position to 256 lines before end of file
f.open("demodoutcarr.txt");
f.seekg(0, ios::end);
pos = f.tellg();
pos -= LINE_LEN * NBR_RECORDS;
f.seekg(pos);
}
But just ran into a snag: because Windows uses a CR/LF as a line terminator, and the Mac uses a LF, my line lengths are different between the two. Any suggestions for a cleaner way to do this? This is what I've come up with, but I'm kind of "meh" on it:
std::deque<char> txt;
std::ifstream f ("file.txt", ios::in|ios::ate/*AT End*/);
unsigned at (f.tellg());
unsigned lines (0);
while(f && lines <= 256)
{
f.seekg(--at);
txt.push_front(char(f.get()));
if(txt.front() == '\n') ++lines; //next may be \r but it does not matter
}
std::string text (txt.size(), '\0');
std::string::iterator st = text.begin();
for(std::deque<char>::iterator it = txt.begin(); it != txt.end(); ++it, ++st) *st = *it;