473,396 Members | 2,029 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,396 software developers and data experts.

fgetc() vs. fread()

I'm reading "C: A Reference Manual" but still can't understand a
very basic thing: is there any functional difference between
fgetc/fputc and fread/fwrite (when reading/writing one unsigned
char)?

1. Books and documentation on C tell me fread and fwrite is "binary".
What does binary mean in this? Anything to do with opening a file
in binary mode?

2. I'm also sure I've read somewhere on the net that fread and
fwrite are unbuffered. True or false?

3. I found this when googling:

[ Lawrence Kirby @ comp.lang.c, Sat 5 Feb 2000 ]
....
"In C a stream is a sequence of characters. All read operations are
defined to read data from the stream as if by one or more calls to
fgetc() and all write operations by one or more calls to fputc()."
....

If that's true, I think it would answer the first 2 questions.

Magnus
Apr 12 '06 #1
8 17129
In article <20***************************@cyberspace.org>,
M. =?ISO-8859-1?Q?=C5hman?= <ah***@cyberspace.org> wrote:
2. I'm also sure I've read somewhere on the net that fread and
fwrite are unbuffered. True or false?


More false than true. Buffering is a (modifiable) property of the
stream, and applies no matter which of the standard I/O functions
are used to access it.

Possibly what you were looking at was an item about read() and
write() instead of fread() and fwrite(). read() and write() are
very commonly implemented (e.g., Unix, POSIX), but are not part of
the standard C library [and are relatively unlikely ever to
adopted into C itself.]
--
Prototypes are supertypes of their clones. -- maplesoft
Apr 12 '06 #2
M. Åhman wrote:
I'm reading "C: A Reference Manual" but still can't understand a
very basic thing: is there any functional difference between
fgetc/fputc and fread/fwrite (when reading/writing one unsigned
char)?

1. Books and documentation on C tell me fread and fwrite is "binary".
What does binary mean in this? Anything to do with opening a file
in binary mode?


On systems (like Microsoft Windows) that have different conventions for
text and "binary" files opening a file in binary mode will affect both
fread/fwrite and fgetc/fputc. The expalanation for what you've read is
that the fread/fwrite interface is more suited for processing files
containing binary data, whereas the getc/putc interface is more suited
for processing text files.

For example, the Unix dump command reads user data records from the
(binary-structured) utmp file directly into the corresponding utmp
struct using the following fread call:

struct utmp utmp;
if (fread((char *) &utmp, sizeof (struct utmp), 1, f_utmp) != 1)

while the Unix cat command removes successive newlines from
(text-structured) files using the following getc call:

for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
if (prev == '\n') {
if (ch == '\n') {

Nothing prevents you from processing text files with fread/fwrite (for
example by reading chunks into a large buffer) or binary files with
getc/putc (for example by assembling the data into variables by
hand-crafted code).

--
Diomidis Spinellis
Code Quality: The Open Source Perspective (Addison-Wesley 2006)
http://www.spinellis.gr/codequality
Apr 12 '06 #3
M. Åhman said:

<snip>
3. I found this when googling:

[ Lawrence Kirby @ comp.lang.c, Sat 5 Feb 2000 ]
...
"In C a stream is a sequence of characters. All read operations are
defined to read data from the stream as if by one or more calls to
fgetc() and all write operations by one or more calls to fputc()."
...

If that's true, I think it would answer the first 2 questions.


When Lawrence Kirby says something about C, you can be more sure of its
accuracy than of your ability to walk in a straight line whilst sober.

From a quick glance at your quote, it looks perfectly correct to me.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Apr 13 '06 #4
M. Åhman wrote:

<snip>
...
"In C a stream is a sequence of characters. All read operations are
defined to read data from the stream as if by one or more calls to
fgetc() and all write operations by one or more calls to fputc()."
...


My conception of fread and fgetc is that fgetc will bring about more
I/O operations between memory and disk because it can only read one
byte into memory at one time while fread reading multiple bytes.

Is my opinion right or wrong?

Apr 13 '06 #5
Claude Yih wrote:
M. Åhman wrote:

<snip>
...
"In C a stream is a sequence of characters. All read operations are
defined to read data from the stream as if by one or more calls to
fgetc() and all write operations by one or more calls to fputc()."
...


My conception of fread and fgetc is that fgetc will bring about more
I/O operations between memory and disk because it can only read one
byte into memory at one time while fread reading multiple bytes.

Is my opinion right or wrong?


The C input and output streams are typically buffered. A large chunk is
read from (say) the disk into memory with one operation. Functions
like getc, fgetc, fread, and fgets will pick data from that stream until
it is empty, which will cause another transfer from the disk. Calling
fgetc will incur a function call overhead, but, unless you've disabled a
stream's buffering, certainly not the overhead of a disk operation.

Here is an example from an actual implementation of fgetc. _p is a
pointer to the stream's buffer; _r a count of characters remaining in
the buffer, and __srget a function that fills the buffer.

#define __sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++))

int
fgetc(FILE *fp)
{
return (__sgetc(fp));
}

getc() is typically implemented as a macro, so it won't even incur the
function calling overhead:

#define getc(fp) __sgetc(fp)

On the other hand, in the same implementation, fread will transfer the
data from the stdio buffer to your specified buffer using memcpy, which
can be more efficient than byte-by-byte copies.

size_t
fread(void *buf, size_t size, count, FILE *fp)
{
size_t resid;
char *p;
int r;

if ((resid = count * size) == 0)
return (0);
p = buf;
while (resid > (r = fp->_r)) {
(void)memcpy((void *)p, (void *)fp->_p, (size_t)r);

--
Diomidis Spinellis
Code Quality: The Open Source Perspective (Addison-Wesley 2006)
http://www.spinellis.gr/codequality
Apr 13 '06 #6
On Wed, 12 Apr 2006 23:48:56 +0300, Diomidis Spinellis <dd*@aueb.gr>
wrote:
<snip>
On systems (like Microsoft Windows) that have different conventions for
text and "binary" files opening a file in binary mode will affect both
fread/fwrite and fgetc/fputc. The expalanation for what you've read is
that the fread/fwrite interface is more suited for processing files
containing binary data, whereas the getc/putc interface is more suited
for processing text files.
Exactly. (And very nicely put.)
For example, the Unix dump command reads user data records from the
(binary-structured) utmp file directly into the corresponding utmp
struct using the following fread call:

struct utmp utmp;
if (fread((char *) &utmp, sizeof (struct utmp), 1, f_utmp) != 1)
dump? Why on Earth should dump care about logins? I don't think it
even cares or cared about userids at all, and if it does or did,
/etc/passwd is not and never was fixed format.
while the Unix cat command removes successive newlines from
(text-structured) files using the following getc call:

for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
if (prev == '\n') {
if (ch == '\n') {
GNU cat does this (and several other equally silly things) but no
other Unix that I've ever heard of does.
Nothing prevents you from processing text files with fread/fwrite (for
example by reading chunks into a large buffer) or binary files with
getc/putc (for example by assembling the data into variables by
hand-crafted code).


Now that is true.

- David.Thompson1 at worldnet.att.net
May 1 '06 #7
In article <1i********************************@4ax.com>,
Dave Thompson <da*************@worldnet.att.net> wrote:
dump? Why on Earth should dump care about logins?


To send messages to all logged-in operators, probably.

-- Richard
May 1 '06 #8
On 2006-05-01, Dave Thompson <da*************@worldnet.att.net> wrote:
On Wed, 12 Apr 2006 23:48:56 +0300, Diomidis Spinellis <dd*@aueb.gr>
wrote:
<snip>
On systems (like Microsoft Windows) that have different conventions for
text and "binary" files opening a file in binary mode will affect both
fread/fwrite and fgetc/fputc. The expalanation for what you've read is
that the fread/fwrite interface is more suited for processing files
containing binary data, whereas the getc/putc interface is more suited
for processing text files.

Exactly. (And very nicely put.)
For example, the Unix dump command reads user data records from the
(binary-structured) utmp file directly into the corresponding utmp
struct using the following fread call:

struct utmp utmp;
if (fread((char *) &utmp, sizeof (struct utmp), 1, f_utmp) != 1)

dump? Why on Earth should dump care about logins? I don't think it
even cares or cared about userids at all, and if it does or did,
/etc/passwd is not and never was fixed format.
while the Unix cat command removes successive newlines from
(text-structured) files using the following getc call:

for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
if (prev == '\n') {
if (ch == '\n') {

GNU cat does this (and several other equally silly things) but no
other Unix that I've ever heard of does.


You probably haven't heard of any BSD-derived unixes, or at least
haven't heard of this feature. The -s option (to do so) is present on
AFAICT all such systems. FreeBSD's code analogous to those lines is as
follows:

for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
if (prev == '\n') {
if (sflag) {
if (ch == '\n') {

The flag check has probably been hoisted outside the loop and omitted in
the code that was pasted above.
May 1 '06 #9

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

Similar topics

6
by: Jason K | last post by:
Let me preface this by saying this obviously isn't a C++ *language* issue per se; rather probably an issue relating to quality of implementation, unless I'm just misusing iostream... I wrote a...
2
by: Luc Holland | last post by:
Hey, I'm working on a program that reads a binary file. It's opened with ==== if ((f1=fopen(argv,"rb"))==NULL) { fprintf(stderr,"Error opening %s for reading . . .\n",argv); exit(2); } ====...
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...
13
by: William L. Bahn | last post by:
I'm sure this has been asked before, and I have looked in the FAQ, but I'm looking for an explanation for the following: The functions pairs: gets()/fgets() puts()/fputs() printf()/fprintf()...
6
by: Kobu | last post by:
Do the "larger" input functions like scanf, gets, fgets use fgetc to take input or an operating system call function like read() (I know it could be any "way", but I'm trying to find out how it's...
3
by: cinsky | last post by:
Hi, While reading ISO C Standard, I found follow text in 7.19.8.1: size_t fread(void *restrict ptr, size_t SIZE, ...) ... For each object, SIZE calls are made to the fgetc function and the...
13
by: 010 010 | last post by:
I found this very odd and maybe someone can explain it to me. I was using fread to scan through a binary file and pull bytes out. In the middle of a while loop, for no reason that i could...
4
by: Christopher Benson-Manica | last post by:
In a thread from substantially earlier this week, Harald van D?k <truedfx@gmail.comwrote: Being rather pendantic, I decided to try to verify whether this was true. I would appreciate...
9
by: primeSo | last post by:
// FIRST int main(void){ int c, i = 0; char cArray; while( (c = getchar()) != '\n' && c != EOF){ cArray = c; i ++; }
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.