On Aug 7, 10:32 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> JackC wrote:
> > On 7 Aug, 20:45, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> >> JackC wrote:
> >>> My program takes a series of encoded file segments, and decodes each
> >>> segment and writes it to the output file.
> >>> [..]
> >>> The problem is that if I don't open the file in append mode
> >>> (ios::app) then it automatically overwrites the output file
> >>> contents, so all previous written segments are deleted. And if i
> >>> use append mode, then seekp doesn't seem to work, and after the
> >>> truncate() is performed, the segments just get appended onto the
> >>> end of the file instead of overwriting the null bytes at the
> >>> position specified.
> >> Have you tried opening in read+write mode?
> >>> [..]
> > Thanks, How exactly would i do that with ofstream? I have tried using
> > ios::out but with no success is that the mode you meant?
> Obviously you can't do that with an *o*fstream.
Why not? According to the standard:
std::ofstream f( filename, std::ios::in ) ;
is supposed to work. In such cases, I'd explicitly or in
std::ios::out as well, just to be clearer, even though
ofstream::open does it for you. In fact, I'd probably add some
sort of comment to the effect that I was doing this because it
is the only way to get an update mode, and not because I wanted
to read.
Also, of course, seeking to arbitrary positions only works if
the file is opened in binary. So the final declaration would
actually be:
std::ofstream f( filename,
std::ios::out | std::ios::in |
std::ios::binary ) ;
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34