Topic: good for open


Author: "P.J. Plauger" <pjp@dinkumware.com>
Date: 1998/07/20
Raw View
Nathan Myers <ncm@nospam.cantrip.org> wrote in article <6oqobq$34j$1@shell7.ba.best.com>...
>   call f.clear()
>   call f.open()
>   compare the result of f.open() against 0, or call is_open().

f.open() returns void.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
---
[ 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/20
Raw View
P.J. Plauger<pjp@dinkumware.com> wrote:
>Nathan Myers <ncm@nospam.cantrip.org> wrote in article <6oqobq$34j$1@shell7.ba.best.com>...
>>   call f.clear()
>>   call f.open()
>>   compare the result of f.open() against 0, or call is_open().
>
>f.open() returns void.

Right, I was looking at filebuf::open().  fstream::open sets
failbit on failure.  It doesn't say that it checks failbit before
it tries to open, or that it clears it on success.

It may be that this will change.  In the meantime, clearing failbit
yourself before calling fstream::open is prudent.

--
Nathan Myers
ncm@nospam.cantrip.org  http://www.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
>You certainly need to say f.clear() to make other things
>(not the open itself) work.  This might be considered an
>omission in the standard.
>
>Most stream operations fail if failbit is set; you might
>reasonably expect fstream::open() to fail as well, but
>the standard doesn't say so.  You might instead reasonably
>expect that failbit (and badbit, and maybe eofbit) are
>cleared on a successful open, but it doesn't say that
>either.
>
>Probably it should say one or the other.  In the meantime,
>it would be prudent to call clear() before reopening an
>fstream.

So the open works even if failbit is set (and continue to be),
so SHOULD the normal way to test the success of opening a file
be to use is_open() ? I don't remember seing that in any book,
everyone test "if (f)" or directly f.fail().

Maybe I could start the trend, I'm just finishing to write a book!

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/18
Raw View
Michel Michaud<Michel.Michaud@sympatico.ca> wrote:
>>You certainly need to say f.clear() to make other things
>>(not the open itself) work.  This might be considered an
>>omission in the standard.  ... In the meantime,
>>it would be prudent to call clear() before reopening an
>>fstream.
>
>So the open works even if failbit is set (and continue to be),
>so SHOULD the normal way to test the success of opening a file
>be to use is_open() ? I don't remember seing that in any book,
>everyone test "if (f)" or directly f.fail().

The normal way should be:

  call f.clear()
  call f.open()
  compare the result of f.open() against 0, or call is_open().

The description of fstream::open() doesn't mention failbit anywhere.

--
Nathan Myers
ncm@nospam.cantrip.org  http://www.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/19
Raw View
>The normal way should be:
>
>  call f.clear()
>  call f.open()
>  compare the result of f.open() against 0, or call is_open().
>
>The description of fstream::open() doesn't mention failbit anywhere.
>
>--
>Nathan Myers

What are you reading ?
Here's what I see in CD2 :

[ mode note: CD2 is out of date, and the Final Draft differs
 from it in many details. See the FAQ for more information.
 And we're still waiting to hear from ISO whether the final
 vote passed. Sigh. -sdc }

27.8.1.13  Member functions                      [lib.fstream.members]
  ...
  void open(const char* s, ios_base::openmode mode);
  Effects:
    Calls   rdbuf()->open(s,mode),  If  that  function  returns  a  null
    pointer, calls setstate(failbit), (which may  throw  ios_base::fail-
    ure).  (_lib.iostate.flags_) )

So f.open returns void ! and it can set failbit...

So the question remains...

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: "Michel Michaud" <Michel.Michaud@sympatico.ca>
Date: 1998/07/17
Raw View
Is it necessary for a stream to be in good() condition
to be able to open it ?
For example :

     fstream f("data", ios_base::in);
     while (f >> /* read content */)
         {
      // use...
   }

     // Here good() is false because eof() is true

  f.close(); // to me, this should start anew
  if (f.is_open()) cout << "How come";
  f.open("data", ios_base::out|ios_base::app);
  if (!f.is_open()) cout << "Surprise";

The close works (is_open() is false) but the open
fails (I get the surprise).

I must add f.clear() (before or after close) to make
things work...

Is this normal, or is it a bug or
a "the standard wasnt ready" in VC 5 ?

I find nothing in CD2 that says open fails if the stream
is not good...

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: "P.J. Plauger" <pjp@dinkumware.com>
Date: 1998/07/17
Raw View
Michel Michaud <Michel.Michaud@sympatico.ca> wrote in article <G2Er1.782$x51.5556179@news21.bellglobal.com>...
> Is it necessary for a stream to be in good() condition
> to be able to open it ?
> For example :
>
>      fstream f("data", ios_base::in);
>      while (f >> /* read content */)
>          {
>       // use...
>    }
>
>      // Here good() is false because eof() is true
>
>   f.close(); // to me, this should start anew
>   if (f.is_open()) cout << "How come";
>   f.open("data", ios_base::out|ios_base::app);
>   if (!f.is_open()) cout << "Surprise";
>
> The close works (is_open() is false) but the open
> fails (I get the surprise).

Interesting. I don't.

> I must add f.clear() (before or after close) to make
> things work...
>
> Is this normal, or is it a bug or
> a "the standard wasnt ready" in VC 5 ?

None of the above, unless you can give me a more complete test case
that fails when I try it. I don't know of any changes to the draft C++
Standard in this area since VC++ V5.0 froze, either.

> I find nothing in CD2 that says open fails if the stream
> is not good...

And I find nothing in the code that should make the open fail.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com



[ 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/17
Raw View
P.J. Plauger<pjp@dinkumware.com> wrote:
>Michel Michaud <Michel.Michaud@sympatico.ca> wrote in article <G2Er1.782$x51.5556179@news21.bellglobal.com>...
>> Is it necessary for a stream to be in good() condition
>> to be able to open it ?
>> For example :
>>
>>      fstream f("data", ios_base::in);
>>      while (f >> /* read content */)
>>          {
>>       // use...
>>    }
>>
>>      // Here good() is false because eof() is true
>>
>>   f.close(); // to me, this should start anew
>>   if (f.is_open()) cout << "How come";
>>   f.open("data", ios_base::out|ios_base::app);
>>   if (!f.is_open()) cout << "Surprise";
>>
>> The close works (is_open() is false) but the open
>> fails (I get the surprise).
>>
>> I must add f.clear() (before or after close) to make
>> things work...

You certainly need to say f.clear() to make other things
(not the open itself) work.  This might be considered an
omission in the standard.

Most stream operations fail if failbit is set; you might
reasonably expect fstream::open() to fail as well, but
the standard doesn't say so.  You might instead reasonably
expect that failbit (and badbit, and maybe eofbit) are
cleared on a successful open, but it doesn't say that
either.

Probably it should say one or the other.  In the meantime,
it would be prudent to call clear() before reopening an
fstream.

--
Nathan Myers
ncm@nospam.cantrip.org  http://www.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              ]