Topic: close/open and eof
Author: jkanze@otelo.ibmmail.com
Date: 1998/07/22 Raw View
In article <6p2uvm$ocf$1@shell7.ba.best.com>,
ncm@nospam.cantrip.org (Nathan Myers) wrote:
> Michel Michaud <Michel.Michaud@sympatico.ca> wrote:
> >>Should close and open clear eof/fail ?
> >
> >(I don't own other compilers so I'd be interested to know what
> >happens with any other than VC...)
>
> Who cares? Whatever happens on some particular version of some
> particular compiler won't be portable to the next version,
> never mind the next system.
I suspect that this really depends on whether the vendor has any
respect for its customers or not. Serious vendors will NOT break
existing code if they can help it, and the de facto standard for
this is for close to reset the fail bit.
> Clear the bits before you call open,
> and your code will not fail for this reason, regardless of what
> each vendor chose to do in that version.
Not much help for existing code. You seem to forget that some of
us have been using C++ for a considerable amount of time now.
> The standard is clear: a conforming library doesn't clear any bits
> without being asked. Not everybody agrees that this is best, but
> nobody disagrees about what it says.
And that it is too late to change it. Any speculation as to why
the standard decided to break existing code. (Generally, when the
standard decided to break existing code, the pay-off was considered
very big. What's the win here.)
--
James Kanze +33 (0)1 39 23 84 71 mailto: kanze@gabi-soft.fr
+49 (0)69 66 45 33 10 mailto: jkanze@otelo.ibmmail.com
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
Conseils en informatique orient e objet --
-- Beratung in objektorientierter Datenverarbeitung
-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Michel Michaud <Michel.Michaud@sympatico.ca>
Date: 1998/07/21 Raw View
>Should close and open clear eof/fail ?
>
>For example :
> fstream f("data", ios_base::in);
> while (f >> /* read content */)
> {
> // use...
> }
> // fail() and eof() are true
> f.close(); // to me, this should start anew
> if (!f.is_open()) cout << "Ok we're closed!";
> f.open("data", ios_base::out|ios_base::app);
> if (f.fail()) cout << "Surprise";
> if (f.is_open()) cout << "But open ok !";
> f << /* write something */;
> if (f.fail()) cout << "Did not write of course";
>
>I must add f.clear() (almost anywhere) to make
>things work...
Interestingly enough what this program does is different
with <fstream.h> than with <fstream> in VC 5. With <fstream.h>
the failbit is cleared after the close that succeeded...
I know there is no need for both to work the same, but maybe
this behavior should have been viewed as "legacy" code, I don't
have them on my machine, but I suspect that Turbo C++ and Borland C++
did the same, because I have old examples that I used back then. It is
not clear to me if people will be happy with "normal" program breaking
under the new standard because we now have "better" rules. I really
don't see why close (an/or open) should not put clearly their
result in the state bits. Don't tell me it is the way it works
in general and it's legacy from errno, etc., would want to test
for an error in previous operation after the file
is closed than reopened?
(I don't own other compilers so I'd be interested to know what
happens with any other than VC...)
Michel Michaud micm19@mail2.cstjean.qc.ca
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: ncm@nospam.cantrip.org (Nathan Myers)
Date: 1998/07/21 Raw View
Michel Michaud <Michel.Michaud@sympatico.ca> wrote:
>>Should close and open clear eof/fail ?
>
>(I don't own other compilers so I'd be interested to know what
>happens with any other than VC...)
Who cares? Whatever happens on some particular version of some
particular compiler won't be portable to the next version,
never mind the next system. Clear the bits before you call open,
and your code will not fail for this reason, regardless of what
each vendor chose to do in that version.
The standard is clear: a conforming library doesn't clear any bits
without being asked. Not everybody agrees that this is best, but
nobody disagrees about what it says.
Nathan Myers
ncm@nospam.cantrip.org
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Michel Michaud" <Michel.Michaud@sympatico.ca>
Date: 1998/07/18 Raw View
Forget my last message, I changed my test but
did not run it !
My new message :
--------------------------------------------------------
Should close and open clear eof/fail ?
For example :
fstream f("data", ios_base::in);
while (f >> /* read content */)
{
// use...
}
// fail() and eof() are true
f.close(); // to me, this should start anew
if (!f.is_open()) cout << "Ok we're closed!";
f.open("data", ios_base::out|ios_base::app);
if (f.fail()) cout << "Surprise";
if (f.is_open()) cout << "But open ok !";
f << /* write something */;
if (f.fail()) cout << "Did not write of course";
I must add f.clear() (almost anywhere) to make
things work...
Is this normal, or is it a bug or
a "the standard wasnt ready" in VC 5 ?
I find clear really annoying when working
with a file (I don't believe in software fixing
hardware) but I could get used to it...
Michel Michaud micm19@mail2.cstjean.qc.ca
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]