seeking in binary files

R

REH

I thought this would have been easy...

Is there a portable way to seek to the end of an open, binary file?

Thanks,

REH
 
B

Ben Pfaff

REH said:
Is there a portable way to seek to the end of an open, binary file?

No, assuming that you don't know the file's length.
Quoting C99, section 7.19.9.2:

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

Lawrence Kirby

No, assuming that you don't know the file's length.
Quoting C99, section 7.19.9.2:

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

On the other hand of an implementation doesn't support that it likely
means that it isn't an easy or sensible operation. This is almost
certainly the nearest thing that exists to a portable solution.

If a "file" isn't seekable it won't meaningfully support *any* seek
operations, standard or otherwise.

Lawrence
 
D

Dik T. Winter

>
> On the other hand of an implementation doesn't support that it likely
> means that it isn't an easy or sensible operation.

Indeed, there are anough binary streams where it makes no sense. Think
sockets or pipes or whatever.
 
J

Jean-Claude Arbaut

Indeed, there are anough binary streams where it makes no sense. Think
sockets or pipes or whatever.

So that depends on what your application is meant to deal with. Anyway, you
should not ask for the size of a socket or a pipe. And if fseek returns a
sensible error value, you know it has failed, at least.
 
L

Lawrence Kirby

On Wed, 22 Jun 2005 03:26:58 +0200, Jean-Claude Arbaut wrote:

....
So that depends on what your application is meant to deal with. Anyway, you
should not ask for the size of a socket or a pipe.

In standard C you can't tell whether it is a socket or a pipe.
And if fseek returns a
sensible error value, you know it has failed, at least.

That's a good point, there is an important difference between
failing an operation with a proper failure indication and performing an
operation "successfully" but in a way that is not "meaningful".

Lawrence
 
R

REH

Lawrence Kirby said:
On Wed, 22 Jun 2005 03:26:58 +0200, Jean-Claude Arbaut wrote:

...


In standard C you can't tell whether it is a socket or a pipe.

I believe you can. Any seek will fail on a socket or pipe. If you seek to
the start of the "file" and it fails, you will get an errno value of ESPIPE,
which indicates an invalid seek.

REH
 
R

Richard Tobin

Indeed, there are anough binary streams where it makes no sense. Think
sockets or pipes or whatever.

Why are binary streams any different from text streams in that respect?
You can't seek on a text socket or pipe either.

-- Richard
 
B

Ben Pfaff

REH said:
I believe you can. Any seek will fail on a socket or pipe. If you seek to
the start of the "file" and it fails, you will get an errno value of ESPIPE,
which indicates an invalid seek.

A strictly conforming program cannot check for an errno value of
ESPIPE.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top