Hello Gianni!
Gianni said:
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.
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.unformatted) paragraph 30.
Unfortunately, this is not the case, a call to std::istream::read 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::read() has
read:
std::istream::gcount() provides the number of characters read by the
last
unformatted input function (27.6.1.3, lib.istream.unformatted,
paragraph 1).
of course. If the
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::gcount() 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.get.area, paragraph 6, and 27.4.4.2,
lib.basic.ios.members, paragraph 4).
... snip
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::readsome(). 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.
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.