ANSI C - Filsize ?

J

Jan Engelhardt

why can't it just skip these null characters until it reaches EOF,
Chris Torek wrote:
CT>On systems that really do this to you -- such as VMS or CP/M --
CT>"system-supplied" null characters are indistinguishable from
CT>user-supplied null characters. For instance:
CT>
CT> FILE *fp = fopen("somefile", "wb");
CT> if (fp != NULL) {
CT> putc(100, fp);
CT> putc(0, fp);
CT> fclose(fp);
CT> }
CT>
CT>will, on VMS, give you a 512-byte file consisting of the byte 100
CT>decimal (0x64), followed by 511 null (0x00) bytes. Remove the
CT>second putc(), and you get an *identical* 512-byte-long file. The
CT>problem then becomes deciding how many null bytes to skip.

Silly VMS.

Lew Pitcher wrote:
LP>Let's pretend that you have a CP/M text file (10 bytes of text) that you
LP>wish to find the length of in a conforming C program. You
LP>fseek(file,0,SEEK_END) on the file, opened as binary, and
LP> a) are positioned 11 bytes into the file, at the 0x1a character that
LP> demarks logical end of file, or
LP> b) are positioned 1024 bytes into the file, at the end of the last (and
LP> first) physical block that the file occupies.
LP>Which is correct to you?

b).
 
I

Irrwahn Grausewitz

225) Setting the file position indicator to end-of-file, as with fseek(file,
0, SEEK_END), has undefined behavior for a binary stream (because of
possible trailing null characters) or for any stream with state-dependent
encoding that does not assuredly end in the initial shift state.

The reason the Standard doesn't say it is that footnotes (of which this is
one) are non-normative - which basically means that they don't count.
Having said that, it would IMHO be folly for a C programmer to ignore a
footnote like this one.

Seconded. Just to add that there's the following wording in
normative text:

C99 7.19.9.2 The fseek function
[...]
3 [...] A binary stream need not meaningfully support fseek calls
with a whence value of SEEK_END.

I wonder if "... need not meaningfully support ..." includes
the possibility of undefined behavior as well.

Regards
 
L

lawrence.jones

Irrwahn Grausewitz said:
I wonder if "... need not meaningfully support ..." includes
the possibility of undefined behavior as well.

I don't think so. I think the footnote overstates the case and should
have said "unspecified behavior" instead.

-Larry Jones

You should see me when I lose in real life! -- Calvin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,139
Messages
2,570,806
Members
47,353
Latest member
TamiPutnam

Latest Threads

Top