Article View: comp.std.c
Article #3573Re: fwrite(), fread() portability.
From: Alan Stokes
Date: Mon, 31 Jul 1995 12:35
Date: Mon, 31 Jul 1995 12:35
48 lines
2532 bytes
2532 bytes
In <danpop.807025255@rscernix> Dan.Pop@mail.cern.ch (Dan Pop) writes: >In <3vav3a$4bi@lyra.csx.cam.ac.uk> nmm1@cus.cam.ac.uk (Nick Maclaren) writes: >>In article <3vat5j$kak@engnews2.Eng.Sun.COM>, Casper.Dik@Holland.Sun.COM (Casper H.S. Dik - Network Security Engineer) writes: >>|> >>|> How can fread/fwrite convert numbers? They don't get numbers, they >>|> get n items of known size but unknown type. >>|> >>|> It doesn't know anything about the data and can therefor do >>|> no conversions. >> >>Take, for example, a 4-byte quantity, and assume 8-bit bytes. This can >>be written out as 4 characters (with the binary code of each character >>the same as the binary code of each byte), or as 8 characters of >>hexadecimal, or as 11 characters of octal, or as 12 characters of octal >>corresponding to the value of each byte. Variations including prefixing >>this data by the length of the item (i.e. 4) in any of the above formats. >All this is irrelevant. Not necessarily. In particular, you can't fwrite something on one machine and have it fread on another, even if the representations of the something are the same, because of the above possibilities. > fread is supposed to read data written by >fwrite Agreed, probably. >and fwrite will write a 4-byte quantity as 4 characters in the >file. Says who? I don't see anything in the standard that requires this. >fread will read the same 4 characters and store them at the >required address. fread must read what fwrite wrote (actually I can't see where the standard explicitly guarantees that, but I think it's implied). >Neither fwrite nor fread don't know or care about the format of the >data or any other details apart from the number of characters which >have to be written or read. Everything else is programmer's >responsibility, because s/he's supposed to know what is the >meaning/format/encoding/etc of the data manipulated with fread/fwrite. But fread and fwrite are apparently free to add their own encoding. -- Alan Stokes (alan@rcp.co.uk) RCP Consultants Ltd Didcot, UK
Technical Details
Message-ID:
<rZQHwACMBh107h@rcp.co.uk>Path:
arc1-ca.pugleaf.net!kate.ibmpcug.co.uk!rcp!alanReferences:
<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>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!kate.ibmpcug.co.uk!rcp!alan From: alan@rcp.co.uk (Alan Stokes) Newsgroups: comp.std.c Subject: Re: fwrite(), fread() portability. Message-ID: <rZQHwACMBh107h@rcp.co.uk> Date: Mon, 31 Jul 95 12:35:55 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> Organization: RCP Consultants Ltd.