fwrite/fread and unbuffered IO

Joel Sherrill <joel@OARcorp.com> joel.sherrill@OARcorp.com
Fri Sep 17 18:32:00 GMT 2004


Is this a termios device by any chance?

Could you try this with read()/write() not their f--- counterparts?
That would narrow the issue down to either RTEMS or newlib.

--joel

Etienne Fortin wrote:
> Hi everyone,
> I'm playing with fread and fwrite and found some strange behaviors.
> Well, behavior that I wasn't expecting.
> 
> If I put my FILE in "unbuffered mode" with setvbuf() and I call
> fwrite(), the memory pointer supplied to fwrite() is directly used as
> the source of the bytes being written to the stream. This is exactly
> what I expected since I'm unbuffered, ie no transit via some other
> buffer.
> 
> But with fread, things are quite different. Let's say I'm unbuffered and
> I write something like this:
> 
>     fread(data, 1, 50, fp);
> 
> I expect fread() to takes the "data" ptr directly as the container of
> the received bytes. But strangely, instead, fread() use the 1 bytes
> buffer embedded in the FILE structure (_ubuf) as a transit buffer for
> the received characters. This is absolutely not the behavior I expected.
> If I tell the FILE stream to be unbuffered, I don't expect my data to
> transit via some buffer, even if that buffer is as small as one byte.
> It's still a buffer. Also, this behavior is quite inneficient since the
> driver function responsible for getting characters from the stream is
> called the number of bytes needed, in the example 50 times!!! Unbuffered
> IO doesn't mean a device can't read bytes in sequence...
> 
> Am I missing something here or is this behavior strange?
> 
> Etienne Fortin
> Sensio
> 
> 


-- 
Joel Sherrill, Ph.D.             Director of Research & Development
joel@OARcorp.com                 On-Line Applications Research
Ask me about RTEMS: a free RTOS  Huntsville AL 35805
    Support Available             (256) 722-9985



More information about the Newlib mailing list