Need Inputs for reading file from specific offset

Can you correct me where i'm going wrong.

Requirement.

1. Need to open existing log file.

2. make note of current end of file(ref1).

3. call_processing_routine (this results in the log file writing ).

4. now read the log file begging at ref1(earlier eof) till the current eof.

Wrote this apparently this is not working.


using namespace std;
#include<iostream>


#include <fstream>

int main () {
string STRING;
fstream infile;
infile.open ("example.txt", ios::in);// | ios::nocreate);// | ios::ate );
if(!infile.is_open()) {
cout<<"error opening\n";
exit (1);
}
infile.seekp(0,ios::end);
getchar(); //During this will open the example.txt and add contents
while(infile.good()) {
getline(infile,STRING);
cout<<STRING<<"\n";
}
infile.close();
}

Last edited on
you need tellp() to get the current offset (after seekp()).
After the call of getchar() you need seekg() with the value of the previous tellp() in order to set the read pointer of the stream
Topic archived. No new replies allowed.