Topic: Streams and openmode


Author: "Scott Robert Ladd" <scott@coyotegulch.com>
Date: Sun, 29 Apr 2001 23:03:46 GMT
Raw View
"James Kuyper Jr." <kuyper@wizard.net> wrote
> Would source.rdbuf.in_avail()>=0 be a useful substitute?

I'll have to try that... it might solve one of my two problems. I also want
to check if the stream argument is opened in binary mode; again, I don't see
any way to query a stream for ios_base::binary.

Of course, I could just be paranoid here -- but we're distributing a library
to external clients, and one of them has already passed an text-translation
iostream into our routine, which, of course, performs binary I/O. If the
other guy had *read* the documentation, he'd have known he needed to pass my
routine a stream with ios_base::binary | ios_base::in....

....but then again, who reads docs, right? ;)

Back to Standard C++: It seems to me that streams *should* support the
interrogation of openmode. Does this count as a new feature or a defect
report?

--
Scott Robert Ladd
Master of Complexity, Destroyer of Order & Chaos
http://www.coyotegulch.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://www.research.att.com/~austern/csc/faq.html                ]





Author: "Markus Schaaf" <m.schaaf.exp-jun2001@gmx.de>
Date: Sun, 29 Apr 2001 23:43:43 GMT
Raw View
"James Kuyper Jr." <kuyper@wizard.net> wrote:

> Would source.rdbuf.in_avail()>=0 be a useful substitute?

If I get the standard's wording right, it would not, because
'in_avail' (i.e. 'showmanyc') may always return 0, even if
there is no read position available. If it returns a positive
value, then you are guaranteed to successfully call 'sgetc'
that many times. If it returns -1, then you can be sure, that
there is nothing left to read. This seems to be useful only
for some kind of optimization. But it may also return 0, which
means you can't assume anything.

Considering ease of implementation and typical laziness of
programmers too, I'd even expect 'in_avail' to return 0.
I've just tested g++ 2.95.2 (usual library) and M$VC
(Dinkumware library). No surprises here. :-)

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: "Scott Robert Ladd" <scott@coyotegulch.com>
Date: Sun, 29 Apr 2001 13:19:46 GMT
Raw View
Perhaps I'm mising something.

Consider the following function:

void readfrom(istream & source)
{
    // read something from source...
}

How do I confirm that source was indeed opened for input? I've been over and
over the Standard, but I cannot find *any* way to query the openmode for a
stream. I can query the flags (ios_base::hex, etc.), but not the mode
(ios_base::in, in this case).

If there is no way to query the open mode of the stream, can somebody
explain *why* this capability was omitted?

Is there some other technique I can use to guarantee that a stream is indeed
opened for input?

--
Scott Robert Ladd
Master of Complexity, Destroyer of Order & Chaos
http://www.coyotegulch.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://www.research.att.com/~austern/csc/faq.html                ]





Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Sun, 29 Apr 2001 18:33:58 GMT
Raw View
Scott Robert Ladd wrote:
>
> Perhaps I'm mising something.
>
> Consider the following function:
>
> void readfrom(istream & source)
> {
>     // read something from source...
> }
>
> How do I confirm that source was indeed opened for input? I've been over and
> over the Standard, but I cannot find *any* way to query the openmode for a
> stream. I can query the flags (ios_base::hex, etc.), but not the mode
> (ios_base::in, in this case).
>
> If there is no way to query the open mode of the stream, can somebody
> explain *why* this capability was omitted?
>
> Is there some other technique I can use to guarantee that a stream is indeed
> opened for input?

Would source.rdbuf.in_avail()>=0 be a useful substitute?

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]