473,385 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

read vs fread

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.
Nov 14 '05 #1
6 19390
Patrice Kadionik wrote:
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...

Nov 14 '05 #2
Patrice Kadionik <ka******@enseirb.fr> writes:
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.
--
Ben Pfaff
email: bl*@cs.stanford.edu
web: http://benpfaff.org
Nov 14 '05 #3
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.

jacob navia wrote:
Patrice Kadionik wrote:
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...

Nov 14 '05 #4


Patrice Kadionik wrote:
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.

--
Er*********@sun.com

Nov 14 '05 #5
Patrice Kadionik <ka******@enseirb.fr> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6
OK,
sorry...
Pat.
Keith Thompson wrote:
Patrice Kadionik <ka******@enseirb.fr> writes:
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.

Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Alain Lafon | last post by:
Helas, I got something that should be a minor problem, but anyhow it isn't to me right now. A little code fragment: fread(&file_qn, x, 1, fp_q); The corresponding text file looks like...
14
by: spike | last post by:
Im trying to write a program that should read through a binary file searching for the character sequence "\name\" Then it should read the characters following the "\name\" sequence until a NULL...
5
by: David Mathog | last post by:
When reading a binary input stream with fread() one can read N bytes in two ways : count=fread(buffer,1,N,fin); /* N bytes at a time */ or count=fread(buffer,N,1,fin); /* 1 buffer at a...
6
by: ericunfuk | last post by:
Hi ALL, I want to read a binary file(it's pic.tif file, I guess it's binary file?), then write it to a new file), I have several questions about this process: When I use fread() to read a...
20
by: ericunfuk | last post by:
If fseek() always clears EOF, is there a way for me to fread() from an offset of a file and still be able to detect EOF?i.e. withouting using fseek(). I also need to seek to an offset in the file...
3
by: juleigha27 | last post by:
Hi, First off, I want to apologize if this already got posted it seemed like something happened when I tried to post it previously and it didn't work. I am new to file manipulation with c. I...
0
by: Simrat Kaur Sandhu | last post by:
hello friends can anybody tell me how to read data from a wave file.. i have writen code as ... class file { private: FILE *fp; char id; char *sound_buffer; //four bytes to hold 'RIFF' long...
13
by: Yin Zhu | last post by:
Hello, All Maybe this question is not proper to post here:( but anyway would someone help me? I am doing my operating system homework. I know how to use a buffer string to do read and write....
1
by: Sheth Raxit | last post by:
even thred is not part of C-std, i feel its most relavant to comp.lang.c hope it will not off-topic ------- I am having data/or Text file, which is only used for reading by multiplethreads in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.