Hello Gianni!
Gianni Mariani wrote:
Denise Kleingeist wrote:
Gianni Mariani wrote:
What I would like to do is read bytes from a stream, any number and any
time. I would like it to wait until there are any bytes to read.
There is no such functionality - neither in the C library no in the C++
library.
I beg to differ. std::fread does exactly what I want.
OK. I'm assuming I know what you want and I'm certain that this doesn't
work
in either C or C++. Since you disagree, apparently my assumption is
wrong.
Now, lets verify or falsify my assumptions: from the description given
and the
mention of pipes, I assume you want to obtain whatever amount of data
is
available from your source as soon as it becomes available. For
example, if
you had read all available character, you want to wait until new
characters
become available. Say you are using a buffer of 64 characters and 25
become
available, you want the read function to return and report that 25
characters
have become available.
Functionality like this can be implemented on many systems supporting C
or
C++ but neither language provides the necessary facilities to set this
up: you
need to resort to platform specific functionality to do so.
At least, these libraries are not required to do anything like this.
These libraries have been performing exactly this way since I wrote my
first C program over 25 years. I have no idea what you talk about.
I'm talking about C and C++ standards and what I assume you want to do.
Neither C nor C++ offer functionality which reads characters and
returns
whatever is there without hitting EOF or an error. Both standards
define their
I/O operations to either read precisely the number of characters
specified in
their read operations, hit an error, or reach EOF. Neither standard
defines any form of reads which wait until there are characters
available and
just return those. ... unless, of course, EOF is reached or an error is
encountered.
If you disagree, please point me at the section of the C or C++
standard which
states otherwise. For C, I'm looking at 7.19.8.1 (The fread function),
for C++ at
27.6.1.3 (lib.istream.un formatted) paragraph 30.
I want the exact same functionality as cstdio's "fread" but in a
std::istream.
You can use std::istream::r ead() for this. This will, however, block
until
either end of the input stream is reached or the requested number of
characters is received - as is the case for fread(),
Unfortunately, this is not the case, a call to std::istream::r ead will
fail if there is not enough data to fulfill the read request. There is
no way for the istream::read call interface to indicate that it has read
fewer than the requested number of bytes so the last block read will
almost always be unreadable.
There is a way to find out how many characters std::istream::r ead() has
read:
std::istream::g count() provides the number of characters read by the
last
unformatted input function (27.6.1.3, lib.istream.unf ormatted,
paragraph 1).
of course. If the
fread() on your system returns differently than described above, it is
std::fread does operate exactly as I want. The size of the last block
being read is returned. I want to do the same thing with std::istream.
If you want a function which returns the number of characters read
rather
than using std::istream::g count() to determine this number, you can use
std::streambuf: :sgetn(), e.g.:
std::streamsize n = std::cin.rdbuf( )->sgetn(buffer , size);
(see 27.5.2.2.3, lib.streambuf.g et.area, paragraph 6, and 27.4.4.2,
lib.basic.ios.m embers, paragraph 4).
... snip
It appeared at first that "readsome" would do exactly what I wanted
This is rather unlikely: the default implementation of readsome() just
obtains characters which are in the buffer of the underlying stream
buffer.
If there are none, it just returns, indicating that it read no
characers. The
default implementation does never attempt to read any bytes. Classes
derived from std::streambuf can choose to use a different approach but
none of the standard stream buffers does.
That's strange since at least 2 compilers I use do exactly the opposite
of what you say, on regular files no less.
Well, maybe I should have said that std::filebuf is not required to
provide a
different than the default implementation of std::streambuf: :in_avail()
which
is the function used to determine the maximum number of characters
available to std::istream::r eadsome(). In my experience, std::filebuf
does
not provide a different implementation than the default and thus
readsome()
which just read the characters currently stuck in the buffer. Since
your
impression was that readsome() didn't work for gcc, I'd guess that at
least
libstdc++ uses does not override this function.
When
reading from a named pipe on gcc, it returns immediately - no errors,
just immediate return, is that the expected behaviour ?
Yes: there are no characters, i.e. there is nothing to do. You might
want to check gcount() for the number of characters read.
OK - you have affirmed why keep away from iostreams for C++ io. It's
not only I who can't figure out how to use it properly.
I certainly know how to use it properly! ... and I'm certainly not
using stdio
in any code I write although I know that library better than most
others, too.
stdio has too many problems, IMO.
Good luck, Denise.