Re: Programming in standard c



Richard wrote:
user923005 <dcorbit@xxxxxxxxx> writes:
[...]
a generic function cannot produce a reliable answer. I guess that
other people have thought about this question and figured out that a
reliable file size function cannot be written in a generic manner.

Rubbish. All files have a byte size.

How many bytes does /dev/tty hold? How about /dev/zero?
/dev/ptyp0? /dev/random? /dev/poll? CON:? LPT:?

Noone has ever said this. A portable file size function that gives a
correct answer is clearly impossible.

How silly.

"correct" would be platform specific. The API need not be.

One could assign an arbitrary pseudo-size -- minus one,
perhaps -- to the troublesome files, and then declare that
this pseudo-size was the "correct by decree" size of the
file. However, the value would then be useless for the
purpose to which Jacob's "Happy christmas" code wanted: to
determine how many bytes to allocate to hold the file's
content.

And even for an ordinary file whose length can be known,
many systems permit the length to change between the moment
it's queried and the moment the result is used. How many bytes
should you allocate for an in-memory copy of /var/adm/messages?

--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxxx
.



Relevant Pages

  • Re: Programming in standard c
    ... a generic function cannot produce a reliable answer. ... reliable file size function cannot be written in a generic manner. ... That there are some files of which the size cant be determined should not be a reason to not add such a function to the standard. ... I dont expect removeto work on *every* file that exists on a system ...
    (comp.lang.c)
  • Re: Generic Functions
    ... Thanks to Richard and Beliavsky - I'm obviously not the only one who "works" ... I had only been thinking about instances when the generic function was being ... but a guess as to probably intent is all it would be. ... resolved without ambiguity. ...
    (comp.lang.fortran)