This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: fstream bug ??
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Theodore Papadopoulo <Theodore dot Papadopoulo at sophia dot inria dot fr>
- Cc: libstdc++ at gcc dot gnu dot org, Julien Wintz <Julien dot Wintz at sophia dot inria dot fr>
- Date: Wed, 12 May 2010 17:23:14 +0100
- Subject: Re: fstream bug ??
- References: <1268546192.2028.32.camel@sara.home> <4BA46E77.6080507@redhat.com> <1269813166.2249.8.camel@sara.home> <4BB12B74.80703@redhat.com> <4BDD0915.2020301@redhat.com> <1272842221.2000.26.camel@sara.home> <4BE32BEA.9010208@redhat.com> <4BE32F7B.3070001@oracle.com> <4BEAD30B.50706@sophia.inria.fr>
On 12 May 2010 17:10, Theodore Papadopoulo wrote:
> I have a behaviour with g++-4.4.3 that I do not understand...
> The following program does not do what I would expect:
>
> #include <string>
> #include <iostream>
> #include <fstream>
> #include <unistd.h>
>
> int main() {
> ? ?std::string str;
> ? ?std::fstream rw("/tmp/toto");
> ? ?rw >> str;
> ? ?std::cout << str << std::endl;
> ? ?rw << "Streams are complicated." << std::endl;
> ? ?sleep(1);
> ? ?return 0;
> }
>
> This program reads whatever is the first word in the file /tmp/toto but
> never writes
> the "Streams are complicated." (checked with strace).
>
> If I swap the read and the write, ie:
>
> int main() {
> ? ?std::string str;
> ? ?std::fstream rw("/tmp/toto");
> ? ?rw << "Streams are complicated." << std::endl;
> ? ?rw >> str;
> ? ?std::cout << str << std::endl;
> ? ?sleep(1);
> ? ?return 0;
> }
>
> then it writes but never reads...
> I use streams a lot, but it's probably the first time I use bidirectional
> streams, so
> there might be sthg I do not understand, but at first sight it is really
> surprising
> and looks like a bug. Can someone confirm this before I file it in bugzilla
> ??
This is a property of file streams, not streams in general. Filebufs
need a seek (either pubseekoff or pubseekpos) when switching between
reading and writing
e.g.
rw >> str;
std::cout << str << std::endl;
rw.rdbuf()->pubseekoff(0, std::ios::end);
rw << "Streams are complicated." << std::endl;
>
> ? ?Theo.
>