473,698 Members | 2,047 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fseek past the eof

Can anyone tell me what the standard says about using fseek (on a binary
file) to seek past the end-of-file? I can't find anything in my (draft)
copy of the standard, nor in the FAQ.

Thanks
Nov 14 '05 #1
20 15185
Xenos wrote:
Can anyone tell me what the standard says about using fseek (on a binary
file) to seek past the end-of-file? I can't find anything in my (draft)
copy of the standard, nor in the FAQ.

Thanks


I'm curious why you would, and what you would expect to find,
seeking past end-of-file. Perhaps I misunderstand.

Still curious, how would you know where end-of-file is and that you
are seeking past it?

The FAQ not withstanding, the semantics of fseek deal with the
beginning of the file, the end of the file and relative offsets
within the file. The actual address is of type long. Assuming a
properly opened file (FILE *fp) of some size, ..

long bof = 0; /* no calc needed. files begin at 0 */
long eof; /* just define it for now */
if (fseek(fp, 0, SEEK_END) != 0) puts("fseek failed, die"), exit(9);
eof = ftell(fp);

Now eof is actually the file length. It 'points' one byte after the
last byte in the file, actually to where the next byte might be
written.

But 'past end-of-file' has no meaning to me. end-of-file + 3?

--
Joe Wright mailto:jo****** **@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #2
>> Can anyone tell me what the standard says about using fseek (on a binary
file) to seek past the end-of-file? I can't find anything in my (draft)
copy of the standard, nor in the FAQ.

Thanks
I'm curious why you would, and what you would expect to find,
seeking past end-of-file. Perhaps I misunderstand.


Under UNIX it is possible to seek past end-of-file, write something
(which moves the end-of-file to the end of what you wrote), and
later seek again and read it. Reading unwritten data reads back
binary 0 bytes. On some versions of UNIX disk "blocks" that were
wholly unwritten were also not allocated, leaving the possibility
of the gigabyte-sized file that fits on a floppy-sized filesystem.
Still curious, how would you know where end-of-file is and that you
are seeking past it?
Many uses for this odd kind of indexed file don't CARE where the
end-of-file is. You compute a hash code for the data record key,
multiply it by the data block size, and that's where you put the
data. If you want to read such a record, you hash the key, multiply
it by the data block size, and try to read it. If you get all bytes
zero or end-of-file, there is no record written there. Hash collisions
are dealt with by putting multiple entries in a "data block", or
by using more bits in the hash to locate a different block.

The above is a crude description of how a UNIX "dbm" file works.
It typically occupies about 1/4 of the disk space than what you'd
expect based on its size.
beginning of the file, the end of the file and relative offsets
within the file. The actual address is of type long. Assuming a
properly opened file (FILE *fp) of some size, ..

long bof = 0; /* no calc needed. files begin at 0 */
long eof; /* just define it for now */
if (fseek(fp, 0, SEEK_END) != 0) puts("fseek failed, die"), exit(9);
eof = ftell(fp);

Now eof is actually the file length. It 'points' one byte after the
last byte in the file, actually to where the next byte might be
written.

But 'past end-of-file' has no meaning to me. end-of-file + 3?


fseek(fp, 32767, SEEK_END);

Gordon L. Burditt
Nov 14 '05 #3
"Xenos" <do**********@s pamhate.com> wrote:
Can anyone tell me what the standard says about using fseek (on a binary
file) to seek past the end-of-file?


Nothing, AFAICT. Which means that, unless I missed something, it's
undefined behaviour. Which is odd; I'd have expected it to be
unspecified: either return non-zero from fseek(), or allow it and do
something system-specific but non-crashing to the file.

Richard
Nov 14 '05 #4
On Tue, 30 Nov 2004 07:45:09 GMT, Richard Bos
<rl*@hoekstra-uitgeverij.nl> wrote:
"Xenos" <do**********@s pamhate.com> wrote:
Can anyone tell me what the standard says about using fseek (on a binary
file) to seek past the end-of-file?


Nothing, AFAICT. Which means that, unless I missed something, it's
undefined behaviour. Which is odd; I'd have expected it to be
unspecified: either return non-zero from fseek(), or allow it and do
something system-specific but non-crashing to the file.


I hadn't realised that either. It doesn't even say that the behaviour
is undefined or implementation defined (although some implementations of
the library do define it, for instance on many Unix systems where it
allows writes (but not reads) past the end of file leaving 'holes').
For that matter since the type of the offset is (signed) long int it is
theoretically valid to use a negative number with SEEK_SET.

POSIX.1 does specify the action for writing beyond end of file, if one
is using a POSIX-compliant system:

The fseek() function shall allow the file-position indicator to be
set beyond the end of existing data in the file. If data is later
written at this point, subsequent reads of data in the gap shall
return bytes with the value 0 until data is actually written into
the gap.

Chris C
Nov 14 '05 #5
In <41************ ****@news.indiv idual.net> rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
"Xenos" <do**********@s pamhate.com> wrote:
Can anyone tell me what the standard says about using fseek (on a binary
file) to seek past the end-of-file?


Nothing, AFAICT.


That's because you can't read.

2 The fseek function sets the file position indicator for the
stream pointed to by stream.

Which part of this statement doesn't cover the case when the new file
position is beyond the current end of file?

Since *any* fseek call may fail, seeking past the current end of file
*may* fail too.

In other words, the standard doesn't treat seeking past the current end
of file as a special case. It's entirely up to the implementor to decide
whether to support such requests or not.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #6

"Joe Wright" <jo********@com cast.net> wrote in message
news:we******** ************@co mcast.com...
Xenos wrote:
Can anyone tell me what the standard says about using fseek (on a binary
file) to seek past the end-of-file? I can't find anything in my (draft)
copy of the standard, nor in the FAQ.

Thanks


I'm curious why you would, and what you would expect to find,
seeking past end-of-file. Perhaps I misunderstand.

Still curious, how would you know where end-of-file is and that you
are seeking past it?

I'm not. We have some vendor code that does, which is having problems.
Before I talk to them about it, I want to understand the standard. I don't
want to run my mouth without knowing what the hell I'm talking about.

Why do it? It creates what is called a "sparse file," a file with holes.
A lot of executables to it (but for what reason, I do not know). It can be
done on Windows and Unix, but I don't know if its implementation specific or
part of the standard.

Thanks.

Nov 14 '05 #7

"Dan Pop" <Da*****@cern.c h> wrote in message
news:co******** **@sunnews.cern .ch...
In <41************ ****@news.indiv idual.net> rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
In other words, the standard doesn't treat seeking past the current end
of file as a special case. It's entirely up to the implementor to decide
whether to support such requests or not.

Thanks, Dan. That's the information I needed.

DrX
Nov 14 '05 #8
In article <co*********@cu i1.lmms.lmco.co m>,
Xenos <do**********@s pamhate.com> wrote:
Why do it? It creates what is called a "sparse file," a file with holes.
A lot of executables to it (but for what reason, I do not know). It can be
done on Windows and Unix, but I don't know if its implementation specific or
part of the standard.


It's not part of the *C* standard, but it's part of Posix, and perhaps
of some other standards. Very few useful programs use only the C
standard.

-- Richard
Nov 14 '05 #9
On Tue, 30 Nov 2004 10:10:56 -0500, Xenos
<do**********@s pamhate.com> wrote:
Why do it? It creates what is called a "sparse file," a file with holes.
A lot of executables to it (but for what reason, I do not know). It can be
done on Windows and Unix, but I don't know if its implementation specific or
part of the standard.


It's part of the POSIX.1 standard, not the C standard. It doesn't
guarantee that there are 'holes' (not possible on some filesystems, for
example FAT) but it does guarantee that:

The fseek() function shall allow the file-position indicator to be
set beyond the end of existing data in the file. If data is later
written at this point, subsequent reads of data in the gap shall
return bytes with the value 0 until data is actually written into
the gap.

(POSIX.1, IEEE Std 1003.1, 2004 Edition)

Win32 has a POSIX layer, I don't know how standard compliant it is.

Chris C
Nov 14 '05 #10

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

Similar topics

62
6223
by: Christopher Benson-Manica | last post by:
On thinking about the "replace a word in a file" thread, I wondered how easy it would be to accomplish the same thing with only one file pointer. This led me to some questions... "For a text stream, offset must be zero, or a value returned by ftell (in which case origin must be SEEK_SET)." If offset is a value returned by ftell (which returns the current file position), and origin is SEEK_SET, then fseek() sets the position to the...
15
16083
by: TJ Walls | last post by:
Hello All, I am baffled ... I am trying to improve the speed of a program that I have written that performs random access within a file. It relies heavily on fseek and is very slow. To test, I wrote the following test program which just writes the numbers 1-167721 sequentially to a binary file: #include <stdio.h> #include <stdlib.h>
10
12891
by: Orion | last post by:
Hey, I was wondering if it was possible to determine if you hit 'EOF' using fseek? I'm using fseek to traverse through the file from start to end and capturing the data into a linked list structure. However, my loop doesn't seem to work well - it totally fumbles out actually: while ((a = fseek(fp,0,SEEK_CUR)) == 0){ // code here }
10
5969
by: Kenneth Brody | last post by:
I recently ran into an "issue" related to text files and ftell/fseek, and I'd like to know if it's a bug, or simply an annoying, but still conforming, implementation. The platform is Windows, where text files use CF+LF (0x0d, 0x0a) to mark end-of-line. The file in question, however, was in Unix format, with only LF (0x0a) at the end of each line. First, does the above situation already invoke "implementation defined" or "undefined"...
6
6149
by: ericunfuk | last post by:
A basic question: When the documentation says "fseek() clears EOF indecator, does it mean when you seek over EOF, EOF is no longer at the original position and hence removed?Say, after I seek over the original EOF, when I fread() from a previous position that I know is before the EOF then fread will not be able to tell if it has encountered the original EOF? Thank YOU!
20
7531
by: ericunfuk | last post by:
If fseek() always clears EOF, is there a way for me to fread() from an offset of a file and still be able to detect EOF?i.e. withouting using fseek(). I also need to seek to an offset in the file frequently(forwards and backwards) and do fread() from that offset. Or better still, could anyone let me know some good ways to achieve what I need to do as above?Can I get hold of the file and being able to read in without using fread()? Using...
0
8674
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8893
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4366
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3045
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2328
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.