ANSI/ISO, filesizes, fseek() and SEEK_END 
Author Message
 ANSI/ISO, filesizes, fseek() and SEEK_END

I have some trouble understanding some items of the standard.
ANSI/ISO 9889-1990   7.9.9.2 'the fseek function' says:

-   "A binary stream need not meaningfully support fseek calls with a
whence value of SEEK_END."

-   SEEK_END must be defined anyway according to 7.9.1

I _think_ this all means that that a binary stream may not have an end, as
in e.g. a continuous data channel from an interstellar probe to earth. But
for more normal files which can be guaranteed to have a finite size a
fseek(..., SEEK_END) should be OK.

Now if I'm right a really portable way of determining filesizes can be
done just by fseek()ing and using SEEK_END on a file opened in binary
mode. (The alternative would be by trial and error.)

Now am I right or am I wrong? And if wrong, in what way?

--
Greetings from
 _____
 /_|__| Auke Reitsma, Delft, The Netherlands.
/  | \  -------------------------------------
        Remove SPAMBLOCK from my address ...
--



Tue, 21 Jan 2003 03:00:00 GMT  
 ANSI/ISO, filesizes, fseek() and SEEK_END

Quote:

> I have some trouble understanding some items of the standard.
> ANSI/ISO 9889-1990   7.9.9.2 'the fseek function' says:

> -   "A binary stream need not meaningfully support fseek calls with a
> whence value of SEEK_END."

> -   SEEK_END must be defined anyway according to 7.9.1

> I _think_ this all means that that a binary stream may not have an end, as
> in e.g. a continuous data channel from an interstellar probe to earth. But
> for more normal files which can be guaranteed to have a finite size a
> fseek(..., SEEK_END) should be OK.

Yes, that's one of the possible rationales behind the way the
standard works for SEEK_END on fseek().

Quote:
> Now if I'm right a really portable way of determining filesizes can be
> done just by fseek()ing and using SEEK_END on a file opened in binary
> mode. (The alternative would be by trial and error.)

Well, you might think so.  But that's not what what the Standard
says.  It doesn't say "must support SEEK_END for files of
definite size" or add any other qualifiers like that.  It just
says that SEEK_END need not be supported.  A good implementation
will support SEEK_END wherever practical, of course, but the
implementation has no obligation to do so; it can just fail every
call using SEEK_END if the C library programmer is feeling lazy
that day.
--



Wed, 22 Jan 2003 03:00:00 GMT  
 ANSI/ISO, filesizes, fseek() and SEEK_END

Quote:

> I have some trouble understanding some items of the standard.
> ANSI/ISO 9889-1990   7.9.9.2 'the fseek function' says:

> -   "A binary stream need not meaningfully support fseek calls with a
> whence value of SEEK_END."

> -   SEEK_END must be defined anyway according to 7.9.1

> I _think_ this all means that that a binary stream may not have an end, as
> in e.g. a continuous data channel from an interstellar probe to earth. But
> for more normal files which can be guaranteed to have a finite size a
> fseek(..., SEEK_END) should be OK.

> Now if I'm right a really portable way of determining filesizes can be
> done just by fseek()ing and using SEEK_END on a file opened in binary
> mode. (The alternative would be by trial and error.)

> Now am I right or am I wrong? And if wrong, in what way?

You are, unfortunately, and to my dismay and surprise a couple of weeks
ago, wrong; if you insist on being completely portable, this way of
finding a file size is not portable either, because apparently a system
may decide not to allow SEEK_END for binaries at all. I still can't
understand why not, but that's what the above quotation says.

Richard
--



Wed, 22 Jan 2003 03:00:00 GMT  
 ANSI/ISO, filesizes, fseek() and SEEK_END

Quote:

> -   "A binary stream need not meaningfully support fseek calls with a
> whence value of SEEK_END."
> -   SEEK_END must be defined anyway according to 7.9.1

Of course it must. For text stream usage, e.g.

Quote:
> But for more normal files which can be guaranteed to have a finite
> size a fseek(..., SEEK_END) should be OK.

No. For a start, ANSI C offers no way to distinguish such 'more normal
files' from other, 'less normal' ones. So "you never know what you'll
get", to cite a certain F.Gump, while he was sitting on a park bench...

Quote:
> Now if I'm right a really portable way of determining filesizes can be
> done just by fseek()ing and using SEEK_END on a file opened in binary
> mode.

It's the closest to a portable method that doesn't rely on a
potentially endless fgetc() loop, sure.  But it's not *really*
portable.  Standard C does allow an otherwise nice and sane platform
to define fseek() to always return -1 if whence==SEEK_END, and the
file is binary. It may do that even if it *could* be implemented
properly. It's a quality-of-implementation issue.

--

Even if all the snow were burnt, ashes would remain.
--



Wed, 22 Jan 2003 03:00:00 GMT  
 ANSI/ISO, filesizes, fseek() and SEEK_END

Quote:

> I _think_ this all means that that a binary stream may not have an end, as
> in e.g. a continuous data channel from an interstellar probe to earth.

Actually it means that even if there is an end, the implementation
might not be able to pin it down precisely, due to block padding etc.

Quote:
> Now if I'm right a really portable way of determining filesizes can be
> done just by fseek()ing and using SEEK_END on a file opened in binary
> mode. (The alternative would be by trial and error.)

No.  If the file is static, you can count the bytes as you read
them, until EOF is encountered.  For other kinds of file, the
notion of size might not be meaningful.
--



Wed, 22 Jan 2003 03:00:00 GMT  
 ANSI/ISO, filesizes, fseek() and SEEK_END

Quote:

> I have some trouble understanding some items of the standard.
> ANSI/ISO 9889-1990   7.9.9.2 'the fseek function' says:

> -   "A binary stream need not meaningfully support fseek calls with a
> whence value of SEEK_END."

> -   SEEK_END must be defined anyway according to 7.9.1

> I _think_ this all means that that a binary stream may not have an
> end, as
> in e.g. a continuous data channel from an interstellar probe to earth.

No, what it means is that a fully conforming compiler need not _support_
fseek calls with SEEK_END.  That is, it is not required that a compiler
do something meaningful when you call fseek with SEEK_END.  It's not
that the file doesn't have an end to seek from, it just means that the
compiler may not support seeking from it.  (C99 says the same thing, in
7.19.9.3/3.)

Quote:
> Now if I'm right a really portable way of determining filesizes can be
> done just by fseek()ing and using SEEK_END on a file opened in binary
> mode. (The alternative would be by trial and error.)

> Now am I right or am I wrong? And if wrong, in what way?

But the Standard just said that fseek need not support SEEK_END for
binary streams.  Therefore using it to determine the size of the file is
not guaranteed to be portable.

--

 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ To be refutable is not the least charm of a theory.
\__/ Friedrich Nietzsche
    7 sisters productions / http://www.7sisters.com/
 Web design for the future.
--



Wed, 22 Jan 2003 03:00:00 GMT  
 ANSI/ISO, filesizes, fseek() and SEEK_END

Quote:
> I have some trouble understanding some items of the standard.
> ANSI/ISO 9889-1990   7.9.9.2 'the fseek function' says:

> -   "A binary stream need not meaningfully support fseek calls with a
> whence value of SEEK_END."

> -   SEEK_END must be defined anyway according to 7.9.1

> I _think_ this all means that that a binary stream may not have an end, as
> in e.g. a continuous data channel from an interstellar probe to earth. But
> for more normal files which can be guaranteed to have a finite size a
> fseek(..., SEEK_END) should be OK.

> Now if I'm right a really portable way of determining filesizes can be
> done just by fseek()ing and using SEEK_END on a file opened in binary
> mode. (The alternative would be by trial and error.)

> Now am I right or am I wrong? And if wrong, in what way?

I remembered that the fstat() function also returned some file information.
Don't know if that is portable or not.

Pieter Winter
--



Wed, 22 Jan 2003 03:00:00 GMT  
 ANSI/ISO, filesizes, fseek() and SEEK_END

Quote:

> I remembered that the fstat() function also returned some file
> information.
> Don't know if that is portable or not.

fstat and stat are POSIX compliant, which means that their use is
portable if you're running a system which conforms to POSIX.  They are
not, however, a part of ANSI/ISO C.

--

 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ I'm paranoid.  But am I paranoid enough?
\__/ Louis Wu
    Physics reference / http://www.alcyone.com/max/reference/physics/
 A physics reference.
--



Wed, 22 Jan 2003 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. fseek(stdin,0,SEEK_END)

2. New ANSI C standard ? ANSI vs ISO/IEC

3. fseek problem in ANSI C

4. ISO/ANSI Objective-C standard (was Re: blocking on NSControl)

5. re ansi - iso standard c functions

6. ANSI/ISO C

7. ansi - iso standard c function definitions

8. ansi - iso standard c function definitions

9. Where can I find the ANSI/ISO standard?

10. virtual function implementation in ANSI/ISO C (long)

11. help ANSI/ISO C

12. ANSI & ISO Standards

 

 
Powered by phpBB® Forum Software