This is the mail archive of the
libstdc++@sourceware.cygnus.com
mailing list for the libstdc++ project.
Re: ofstream::seekp(0) does not work
- To: Bruno dot Voigt at ic3s dot de
- Subject: Re: ofstream::seekp(0) does not work
- From: Benjamin Kosnik <bkoz at fidel dot cygnus dot com>
- Date: Wed, 22 Dec 1999 22:43:40 -0800 (PST)
- cc: libstdc++ at sourceware dot cygnus dot com
please post this to the list. We might as well start discussing this
now. It's my opinion that your code is incorrect: that seekp(0) does
not meet the restraints of seekp as defined in the standard.
Where seekp(pos) is undefined if pos is not the result of a previous
tellg or seekoff. It was my choice to enforce this: I know that Plum
Hall does not.
-benjamin
On Wed, 22 Dec 1999 Bruno.Voigt@ic3s.de wrote:
>
> Hello Benjamin,
>
> here is a small sample:
>
> bye, Bruno
> --
> Bruno.Voigt@ic3s.de # IC3S AG # Tel. 04106.655.105 # Fax. 04106.4618
> ____________________ # Mobile 0700.686.00.686
>
> /** testcase for ofstream::seekp().
> * expected content in test.dat: "Header2Record"
> * instead it contains: "Header1RecordHeader2"
> *
> * Bruno.Voigt@ic3s.de / 22.12.1999
> */
> #include <iostream>
> #include <iomanip>
> #include <fstream>
> #include <sstream>
> #include <string>
> #include <cstdlib>
> #include <ctime>
> using namespace std;
>
> ofstream out;
>
>
> bool writeHeader1()
> {
> out.seekp(0);
> const char * header = "Header1";
> out.write(header, strlen(header));
> return true;
> }
> bool writeHeader2()
> {
> out.seekp(0);
> const char * header = "Header2";
> out.write(header, strlen(header));
> return true;
> }
>
> bool writeRecord()
> {
> const char * rec = "Record";
> out.write(rec, strlen(rec));
> return true;
> }
>
> int main(int argc, char *argv[])
> {
> out.open("test.dat", ios_base::out | ios_base::binary | ios_base::trunc);
> if (!out.good()) {
> cout << "cannot open outfile!" << endl;
> exit(1);
> }
> writeHeader1();
> writeRecord();
> writeHeader2();
> out.close();
> return 0;
> }
>
>