🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #3591

Re: fwrite(), fread() portability.

#3591
From: Stephen Baynes
Date: Tue, 01 Aug 1995 07:20
41 lines
2650 bytes
Dan Pop (Dan.Pop@mail.cern.ch) wrote:
: In <rZQHwACMBh107h@rcp.co.uk> alan@rcp.co.uk (Alan Stokes) writes:


: >But fread and fwrite are apparently free to add their own encoding.

: No, they aren't.  From 4.9.3:

:     All input takes place as if characters were read by successive
:     calls to the fgetc function; all output takes place as if characters
:     were written by successive calls to the fputc function.

: So, fwrite(ucharptr, 1, 1, fp) is guaranteed to be equivalent to 
: fputc(*ucharptr, fp).

: If some funny encoding occurs, it happens at a lower level of the
: implementation and it affects all the stdio functions in the same way.

I think that is debatable. It can be interpreted as you suggest and I would
expect all reasonable implementations to do so. However one can also be 
interpreted that fread/fwrite must do all its actual io with fgetc/fputc or
as-if. It does not say what characters are read or written or even how many.
For example extra characters could be used to achive some sort of alignment
or even do error checking.
All it does mean that you could copy the data written with fwrite using 
fgetc/fputc and then reread the copy using fread to get the original values 
but it not say anything about the values you fgetc/fputced except they were 
valid data for fgetc/fputc. To ensure the sensible interpretation something 
like the following would have to be stated by the standard:

    Each object being read or written is treated as an array of characters
of the same size as the object. These characters are read or written with
fgetc or fputc (or as-if) in order of ascending index. The array of objects
forming the parameter is read or written in ascending order of index. No
additional characters are read or written.

--
Stephen Baynes                              baynes@mulsoc2.serigate.philips.nl
Philips Semiconductors Ltd
Southampton                                 My views are my own.
United Kingdom
Technical Details
Message-ID: <DCMF28.2tr@ukpsshp1.serigate.philips.nl>
Path: arc1-ca.pugleaf.net!phcoms4.seri.philips.nl!panther!baynes
References: <3vav3a$4bi@lyra.csx.cam.ac.uk> <3vat5j$kak@engnews2.Eng.Sun.COM> <3vappo$1uj@lyra.csx.cam.ac.uk> <DCFBC9.3Ar@westminster.ac.uk> <danpop.807025255@rscernix> <rZQHwACMBh107h@rcp.co.uk> <danpop.807204213@rscernix>

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c
Path: arc1-ca.pugleaf.net!phcoms4.seri.philips.nl!panther!baynes
From: baynes@ukpsshp1.serigate.philips.nl (Stephen Baynes)
Subject: Re: fwrite(), fread() portability.
Sender: news@ukpsshp1.serigate.philips.nl (account for localnews)
Message-ID: <DCMF28.2tr@ukpsshp1.serigate.philips.nl>
Date: Tue, 1 Aug 1995 07:20:32 GMT
References: <3vav3a$4bi@lyra.csx.cam.ac.uk> <3vat5j$kak@engnews2.Eng.Sun.COM> <3vappo$1uj@lyra.csx.cam.ac.uk> <DCFBC9.3Ar@westminster.ac.uk> <danpop.807025255@rscernix> <rZQHwACMBh107h@rcp.co.uk> <danpop.807204213@rscernix>
Organization: Philips Semiconductors, Southampton, UK
X-Newsreader: TIN [version 1.2 PL2]