I'm trying to append some data to a file, but in some cases want to skip back a bit from the end to overwrite the tail end of the file. However, neither seekp( pos )
nor seekp( offset, relative )
is having any effect for me (except complaining when using a negative offset). Am I using them incorrectly or are they broken?
A little example follows. Compiler: gcc version 4.4.4 (Debian 4.4.4-6)
#include <fstream>
#include <sstream>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace std;
using namespace boost::posix_time;
int main(int nargs, char** pargs){
if( nargs < 2 || nargs > 3 ){
cerr<<"Usage: "<<pargs[0]<<" file [pos]"<<endl;
return 1;
}
const char* fname = pargs[1];
ofstream f( fname, ios::binary|ios::out|ios::ate );
if( !f.good() ){
cerr<<"Unable to open file!"<<endl;
return 1;
}
if( nargs == 3 ){
istringstream offss(pargs[2]);
streamoff off;
offss >> off;
cout <<"Seeking to: "<<off<<endl;
f.seekp( off, ios_base::end ); // using beg or cur instead doesn't help, nor does: seekp( off )
if( f.fail() ){
cerr<<"Unable to seek!"<<endl;
f.clear(); // clear error flags
}
}
f<<"[appending some data at "<<second_clock::local_time()<<"]\n";
return 0;
}
Now, if I seek using a 0 offset, it should place the output-position at the end of the file and writes should append, right? Well, it's having no effect for me (osf was not previously empty):
> ./ostream_app_pos osf 0
Seeking to: 0
> cat osf
[appending some data at 2010-Jul-21 11:16:16]
The usual way of appending is to use ios::app
. In this case, appending works, but trying to seek with a neg/pos offset has no effect, since (from gcc doc):
ios::app Seek to end of file before each write.
I also tried using neither ios::ate
nor ios::app
(presumably truncate mode), to the same effect as ios::ate
.
Sorry if this reads rather like a bug report, but I wanted to check whether there's something I've got wrong here in the usage of seekp
and get an idea of whether it's compiler-specific.