differences between fread and read

Write here if you have problems with your C source code

Moderator: Board moderators

differences between fread and read

Postby CDiMa » Mon Nov 24, 2003 3:36 pm

Hi! :)

When I have to deal with big inputs I usually deal with it reading in a buffer the data:
[c]numread=fread(buffer, sizeof(char), MAXBUF, stdin);[/c]
or:
[c]numread=read(0, buffer, MAXBUF);[/c]
What are the differences between these two?
On my system it looks like read doesn't try to fill the whole buffer, but only gets as far as the first newline :-?.
Does this have to do with some settings of the input stream? May I rely on this behaviour?
Is there a way to explicitly enable/disable it?

Thanks in advance for any suggestion! :roll:

Ciao!!!

Claudio
CDiMa
Experienced poster
 
Posts: 214
Joined: Fri Oct 17, 2003 5:49 pm
Location: Genova

Postby Dominik Michniewski » Mon Nov 24, 2003 4:05 pm

fread() does buffered read, but read() - unbuffered. So read() is called "lower API function" - fread() uses read() and it's only a fascade (I think)

Best reagrds
DM
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)
Dominik Michniewski
Guru
 
Posts: 828
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland

Postby Ivor » Mon Nov 24, 2003 4:30 pm

I'm not going to check it but as I remember read() uses buffer too. There's some sort of constant or setting in lower functions that enables you to control buffering. There's three choices: fully buffered, line buffered and not buffered. Because read() is a system function it can vary a bit in it's declaration (see borland c earlier versions, if I'm correct even declaration of fread() was a bit different, don't remember though, 's been some time since I used that one). Because of that it is advisable to use fread() -- it's ANSI.

I'm not giving my head for the things I wrote. ;)
Ivor
There is a theory which states that if ever anyone discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.
Ivor
Experienced poster
 
Posts: 147
Joined: Wed Dec 26, 2001 2:00 am
Location: Tallinn, Estonia

Postby CDiMa » Tue Nov 25, 2003 11:10 am

Ivor wrote:There's some sort of constant or setting in lower functions that enables you to control buffering. There's three choices: fully buffered, line buffered and not buffered.


Anyone can say more on this? How can one control how read buffers data?

Ciao!!!

Claudio
CDiMa
Experienced poster
 
Posts: 214
Joined: Fri Oct 17, 2003 5:49 pm
Location: Genova

Postby Russell John » Tue Nov 25, 2003 3:08 pm

There are two ways to read in C - fscan and fread... read is like fread in C, to read byte by byte.

[c]inp.read(char *buffer, int n);[/c]
Char *buffer, where to read to, n is number of bytes to read.
1024 Programmer's Lane,
Algorithm City, United States of C++,
Planet Earth.
User avatar
Russell John
New poster
 
Posts: 41
Joined: Mon Jul 21, 2003 9:21 am
Location: Planet Earth

Postby CDiMa » Tue Nov 25, 2003 6:42 pm

Russell John wrote:There are two ways to read in C - fscan and fread... read is like fread in C, to read byte by byte.

[c]inp.read(char *buffer, int n);[/c]
Char *buffer, where to read to, n is number of bytes to read.


Hmmm, I'm not sure if we are talking of the same thing...
On a UNIX system read is a system call to read data from a stream.
In C read is a function provided by the standard C library that takes a file descriptor and uses the read system call to read data.
I don't know what inp.read means, I guess you are talking about C++.

Anyway my problem is very specific.
On my system when I try to read() say 1024 bytes I always get only a few bytes up to the first newline. This suggests that some kind of buffering takes place at a lower level than the C read() function. I'd like to know if there is some way to enable/disable this behaviour.

I hope this is a little bit clearer now...

Ciao!!!

Claudio
CDiMa
Experienced poster
 
Posts: 214
Joined: Fri Oct 17, 2003 5:49 pm
Location: Genova


Return to C

Who is online

Users browsing this forum: No registered users and 0 guests