473,396 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

seeking in binary files

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
Nov 14 '05 #1
8 1903
"REH" <bo***@nowhere.net> writes:
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.
--
"Your correction is 100% correct and 0% helpful. Well done!"
--Richard Heathfield
Nov 14 '05 #2
On Tue, 21 Jun 2005 14:41:00 -0700, Ben Pfaff wrote:
"REH" <bo***@nowhere.net> writes:
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.


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

Nov 14 '05 #3
In article <pa****************************@netactive.co.uk> Lawrence Kirby <lk****@netactive.co.uk> writes:
On Tue, 21 Jun 2005 14:41:00 -0700, Ben Pfaff wrote:
"REH" <bo***@nowhere.net> writes:
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.


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.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 14 '05 #4

On 22/06/2005 03:13, Dik T. Winter wrote:
In article <pa****************************@netactive.co.uk> Lawrence Kirby
<lk****@netactive.co.uk> writes:
On Tue, 21 Jun 2005 14:41:00 -0700, Ben Pfaff wrote:
"REH" <bo***@nowhere.net> writes:

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.


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.


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.

Nov 14 '05 #5
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
Nov 14 '05 #6
REH

"Lawrence Kirby" <lk****@netactive.co.uk> wrote in message
news:pa****************************@netactive.co.u k...
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.


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


Nov 14 '05 #7
In article <II********@cwi.nl>, Dik T. Winter <Di********@cwi.nl> wrote:
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
Nov 15 '05 #8
"REH" <bo***@nowhere.net> writes:
"Lawrence Kirby" <lk****@netactive.co.uk> wrote in message
news:pa****************************@netactive.co.u k...
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.


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.
--
"This is a wonderful answer.
It's off-topic, it's incorrect, and it doesn't answer the question."
--Richard Heathfield
Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Ching-Lung | last post by:
Hi all, I try to create a tool to check the delta (diff) of 2 binaries and create the delta binary. I use binary formatter (serialization) to create the delta binary. It works fine but the...
24
by: Joseph Geretz | last post by:
Up to this point, our application has been using Windows File Sharing to transfer files to and from our application document repository. This approach does not lend itself toward a secure...
10
by: Susan Baker | last post by:
Hi Guys (and girls), I am in a bit of a bind. I'm looking for a simple "proof of concept" C# app (WinForm OR console) that sends a web request to a simple PHP script and receives BINARY data...
8
by: dagecko | last post by:
Hi I would like to know how to detect if a file is binary or not. It's important for me but I don't know where to start. Ty
10
by: joelagnel | last post by:
hi friends, i've been having this confusion for about a year, i want to know the exact difference between text and binary files. using the fwrite function in c, i wrote 2 bytes of integers in...
68
by: vim | last post by:
hello everybody Plz tell the differance between binary file and ascii file............... Thanks in advance vim
3
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its...
3
by: Jia Lu | last post by:
Hello all I see there are lots of flat db or db-like modules in the standard python modules. What about the keywords seeking speed of them ? (I want to put about 10000 articles with 10000...
27
by: Jason | last post by:
Hi, I need to open an existing file, seek to a position at X number of bytes, and write out Y number of bytes overwriting any existing bytes, but no erasing any other data. Is this possible? ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.