473,322 Members | 1,403 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,322 software developers and data experts.

Must fread() use fgetc()?

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 results stored, in the order read, in an array of
unsigned char exactly overlaying the object.

My question is, should any fread() implementation call fgetc()?

Why not getc()? The standard says that getc() might be implemented
in macros. So I think fread() should prefer getc() over fgetc().

Actually, I wonder why Standard mention fgetc() to explain fread()
in the first place. In my opinion, it is enough to say that "read
a character" than "call fgetc()...". No?

If I misunderstood something, please let me know.

Regards,

May 1 '06 #1
3 3271
In article <11**********************@u72g2000cwu.googlegroups .com>,
<ci****@gmail.com> wrote:
size_t fread(void *restrict ptr, size_t SIZE, ...)

... For each object, SIZE calls are made to the fgetc function
and the results stored, in the order read, in an array of
unsigned char exactly overlaying the object.

My question is, should any fread() implementation call fgetc()?
Everything in the standard is covered by the "as-if" rule, so a real
implementation doesn't need to actually implement it like that. It
just has to behave AS IF it was implemented like that.
Why not getc()? The standard says that getc() might be implemented
in macros. So I think fread() should prefer getc() over fgetc().
Real fread() implementations probably don't call either. There's
no way for you to tell (except by reading the source, which doesn't
count).
Actually, I wonder why Standard mention fgetc() to explain fread()
in the first place. In my opinion, it is enough to say that "read
a character" than "call fgetc()...". No?


It's just a way of simplifying the explanation and avoiding
duplication. It means that everything that's required of fgetc()
alos applies to fread().

-- Richard
May 1 '06 #2
On 1 May 2006 06:19:53 -0700, ci****@gmail.com wrote in comp.lang.c:
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 results stored, in the order read, in an array of
unsigned char exactly overlaying the object.

My question is, should any fread() implementation call fgetc()?
All input and output functions in C are defined in terms of calls to
fgetc() and fputc(). They do not actually need to be implemented that
way, they must merely operate "as if" they were implemented by calls
to fgetc() or fputs().
Why not getc()? The standard says that getc() might be implemented
in macros. So I think fread() should prefer getc() over fgetc().
If getc() is implemented as a macro, the standard requires that it
must be equivalent to fgetc().
Actually, I wonder why Standard mention fgetc() to explain fread()
in the first place. In my opinion, it is enough to say that "read
a character" than "call fgetc()...". No?
Once again, the standard defines all stream input and output, whether
performed by functions like fread() and fwrite(), or line oriented
like fgets() and fputs(), or formatted like fscanf() and fprintf(), in
terms of the functions fgetc() and fputs(), respectively.

That means that the features and semantics of reading a character from
a stream, or writing a character to a stream, needs to be defined only
once each, in those two functions. All other functions that read or
write streams operate the same as these two.
If I misunderstood something, please let me know.

Regards,


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
May 1 '06 #3


Jack Klein wrote On 05/01/06 16:55,:
On 1 May 2006 06:19:53 -0700, ci****@gmail.com wrote in comp.lang.c:

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 results stored, in the order read, in an array of
unsigned char exactly overlaying the object.

My question is, should any fread() implementation call fgetc()?

All input and output functions in C are defined in terms of calls to
fgetc() and fputc(). They do not actually need to be implemented that
way, they must merely operate "as if" they were implemented by calls
to fgetc() or fputs().

Why not getc()? The standard says that getc() might be implemented
in macros. So I think fread() should prefer getc() over fgetc().

If getc() is implemented as a macro, the standard requires that it
must be equivalent to fgetc().


Almost, but you left out the crucial loophole starting with
the word "except:"

7.19.7.5/2: "The getc function is equivalent to fgetc,
except that if it is implemented as a macro, it may
evaluate stream more than once, so the argument should
never be an expression with side effects."

In an implementation where fread() is a macro, it must arrange
to evaluate each of its arguments exactly once (7.1.4/1), a
requirement that also applies to fgetc() but not to getc(). I
imagine that the authors of the Standard felt it might make the
document less clear if the fread() definition used getc() and
dragged in the multiple-evaluation hobgoblin; the definition
using fgetc() avoids the potential confusion.

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

May 1 '06 #4

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

Similar topics

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: Patrice Kadionik | last post by:
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,...
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...
4
by: bill | last post by:
I am confused by the behavior of the following code. The function copy() takes two FILE *'s and copies the data from one to the other. In the main routine, I create a pipe and copy stdin to the...
8
by: M. Åhman | last post by:
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)?...
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...
30
by: empriser | last post by:
How to use fread/fwrite copy a file. When reach file's end, fread return 0, I don't konw how many bytes in buf.
4
by: Giacomo | last post by:
Hello.. i'm using php on linux --version: PHP 5.2.5 (cli) (built: Apr 25 2008 18:40:41) Copyright (c) 1997-2007 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies and...
15
by: =?ISO-8859-15?Q?L=E9na=EFc?= Huard | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all, For some reasons, somewhere in a program, I'd like, if possible, to quickly parse a whole file before rewinding it and letting the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.