read vs fread

P

Patrice Kadionik

Hi all,

I want to make a brief comparison between read() and fread() (under a
Linux OS).

1. Family read and Co: open, close, read, write, ioctl...
2. Family fread and Co: fopen, fclose, fread, swrite, fcntl...


Family read and Co:
- are syscalls.
- are not formatted IO : we have a non formatted byte stream.
- don't use the Linux buffer cache.
- generally used for accessing character devices.


Family fread and Co:
- are functions of the standard IO libc (glibc).
- use an internal buffer (in their coding)
- are formatted IO (with the "%.." parameter) for some of them.
- use always the Linux buffer cache.
- generally used for accessing bloc devices.

When I'm opening an ordinary file on a HD (bloc device), I'm always
using the buffer cache with open or fopen. In case of fopen, I'm using
in addition an internal buffer when I'm doing a fread or fwrite for
speeding HD access.
When I'm opening an character device, I'm not using the buffer cache.
Open and Co are generally used here.

Is is OK?
Some points I've forgotten?

Thank you for your response;
Pat.
 
B

Ben Pfaff

Patrice Kadionik said:
I want to make a brief comparison between read() and fread() (under a
Linux OS).

This is not the place to do it. Try a Linux programming
newsgroup.
 
J

jacob navia

Patrice said:
Hi all,

I want to make a brief comparison between read() and fread() (under a
Linux OS).

1. Family read and Co: open, close, read, write, ioctl...
2. Family fread and Co: fopen, fclose, fread, swrite, fcntl...


Family read and Co:
- are syscalls.
- are not formatted IO : we have a non formatted byte stream.
- don't use the Linux buffer cache.
- generally used for accessing character devices.


Family fread and Co:
- are functions of the standard IO libc (glibc).
- use an internal buffer (in their coding)
- are formatted IO (with the "%.." parameter) for some of them.
- use always the Linux buffer cache.
- generally used for accessing bloc devices.

When I'm opening an ordinary file on a HD (bloc device), I'm always
using the buffer cache with open or fopen. In case of fopen, I'm using
in addition an internal buffer when I'm doing a fread or fwrite for
speeding HD access.
When I'm opening an character device, I'm not using the buffer cache.
Open and Co are generally used here.

Is is OK?
Some points I've forgotten?

Thank you for your response;
Pat.


I think that's quite OK. Maybe a small point, you can read
from a character device using fread, or fgetc, for instance
you can read from the keyboard...
 
P

Patrice Kadionik

Hi Jacob,
You're right!
You may use fread on a character device (a serial line for example).
Interesting when the byte stream is ASCII character oriented...
Thanks;
Pat.
 
E

Eric Sosman

Patrice said:
Hi all,

I want to make a brief comparison between read() and fread() (under a
Linux OS).

Only a few of your questions are about the C language,
so I will give very brief answers. For more information,
please try a Linux or Unix newsgroup.
1. Family read and Co: open, close, read, write, ioctl...
2. Family fread and Co: fopen, fclose, fread, swrite, fcntl...

I think "swrite" should be "fwrite." Also, "fcntl" does
not belong in this list.
Family read and Co:
- are syscalls.
- are not formatted IO : we have a non formatted byte stream.
- don't use the Linux buffer cache.

They probably *do* use the buffer cache when reading
and writing disk files.
- generally used for accessing character devices.

No; they can be used with any devices.
Family fread and Co:
- are functions of the standard IO libc (glibc).

They are library functions specified by the C language
Standard.
- use an internal buffer (in their coding)

Usually, but not necessarily.
- are formatted IO (with the "%.." parameter) for some of them.
- use always the Linux buffer cache.

The probably do *not* use the buffer cache when reading
and writing things that are not disk files.
- generally used for accessing bloc devices.

No; they can be used with any devices the implementation
supports.
When I'm opening an ordinary file on a HD (bloc device), I'm always
using the buffer cache with open or fopen. In case of fopen, I'm using
in addition an internal buffer when I'm doing a fread or fwrite for
speeding HD access.

Usually, but not necessarily.
When I'm opening an character device, I'm not using the buffer cache.
Open and Co are generally used here.

You are probably not using the buffer cache, but there
is no problem using fopen() with these devices -- think about
using getchar() to read keyboard input, for example.
Is is OK?
Some points I've forgotten?

comp.unix.programmer, or Linux newsgroups.
 
K

Keith Thompson

Patrice Kadionik said:
I want to make a brief comparison between read() and fread() (under a
Linux OS).

fread() is defined by the C standard; read() is not. For more
details, try a Linux-specific newsgroup, or perhaps
comp.unix.programmer.
 
P

Patrice Kadionik

OK,
sorry...
Pat.
Keith said:
fread() is defined by the C standard; read() is not. For more
details, try a Linux-specific newsgroup, or perhaps
comp.unix.programmer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top