473,654 Members | 3,074 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fseek

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
current position. What is the point of doing so? And, more importantly, why
can't text streams be fseek()'ed randomly like a binary stream can (i.e.,
offset can be any number of bytes)? Do I understand this paragraph correctly?
If so, can fgetpos() and fsetpos() be used to approximate fseek() for text
streams?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 13 '05 #1
62 6213
mf
Christopher Benson-Manica wrote:
"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
current position. What is the point of doing so? And, more importantly, why


It seems to make more sense if you read that as
"or a value previously returned by ftell," so you
may save the ftell value and use it later.

--
Michael
Nov 13 '05 #2
mf <mf@dcs.warwick .ac.uk> spoke thus:
It seems to make more sense if you read that as
"or a value previously returned by ftell," so you
may save the ftell value and use it later.


Aahh... ;( Now *that* makes more sense. Still, why the restriction? Or
rather, why do binary streams get to seek whereever they want, while text
streams are constrained to be proper and seek to someplace that was at least
valid at some point in the past? Maybe I should just go home, eh? ;)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 13 '05 #3
Christopher Benson-Manica wrote:

mf <mf@dcs.warwick .ac.uk> spoke thus:
It seems to make more sense if you read that as
"or a value previously returned by ftell," so you
may save the ftell value and use it later.


Aahh... ;( Now *that* makes more sense. Still, why the restriction? Or
rather, why do binary streams get to seek whereever they want, while text
streams are constrained to be proper and seek to someplace that was at least
valid at some point in the past? Maybe I should just go home, eh? ;)


Usually, people who wonder about this are thinking of
trying to fseek() to an arithmetically-calculated position
in the file: so-and-so many characters before or after such-
and-such a position. But how should this new position be
defined? Where should fseek(stream, 1000, SEEK_SET) land?

- The same position you'd reach by starting at the
beginning and doing getc(stream) 1000 times? This
might take just as long as doing the getc()s, if
there's no way to calculate the destination. For
example, consider reading an MS-DOS file, where
pairs of CR/LF must be translated to single '\n'
characters; the proper destination depends on how
many such pairs are skipped over, and that may not
be knowable without actually reading them.

- 1000 bytes from the start of the file? That might
not even be a valid character position at all; it
might not be possible to arrive at that position
by repeated getc()s. For example, consider reading
an OpenVMS VAR file, where each line consists of a
two-byte count, a "payload," and a possible padding
bytes. Some of those bytes have no "image" in the
data getc() can read, conversely, the '\n' characters
returned by getc() do not actually exist in the file.

There are lots and lots of file organizations out there,
and a requirement to support arbitrary seeking in text streams
would make efficient C implementations difficult or impossible.
One of the reasons fgetpos() and fsetpos() were created was
to give better support to file systems less simple than those
C grew up with.

--
Er*********@sun .com
Nov 13 '05 #4
On Fri, 07 Nov 2003 19:41:27 +0000, Christopher Benson-Manica wrote:
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
current position. What is the point of doing so?
The value returned by ftell could have been from earlier:

get position
read
read
read
seek back to saved position
And, more importantly, why
can't text streams be fseek()'ed randomly like a binary stream can


Try this:

File x contains a series of chars '0', '1', '2' and so on, up to '9'.
Between each is written a "CRLF". In text mode, "CRLF"is treated as "\n"
on read, despite being two bytes in the file and only one after conversion.

Now, when attempting to seek to position n in the file... where n is
specified in "bytes from start of file"... exactly how many such "CRLF"
pairs are there to cope with and how should the offset be altered as a
result? After all, if I fgetc 9 times, the 10th will produce a specific
result; if I fseek to the 10th position and fgetc, I should get the same
result, no? But that would mean that fseek needs to figure out, on the
fly, the contents of the file and what transformations would be done. If
nothing else, it would be hellishly inefficient.
Nov 13 '05 #5
Eric Sosman <Er*********@su n.com> spoke thus:
Usually, people who wonder about this are thinking of
trying to fseek() to an arithmetically-calculated position
in the file: so-and-so many characters before or after such-
and-such a position. But how should this new position be
defined? Where should fseek(stream, 1000, SEEK_SET) land?


Ah, I see now. Far be it from me to inconvenience implementors... ;)
Thanks.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 13 '05 #6
On Mon, 10 Nov 2003 12:44:40 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.c yberspace.org> wrote:
Eric Sosman <Er*********@su n.com> spoke thus:
Usually, people who wonder about this are thinking of
trying to fseek() to an arithmetically-calculated position
in the file: so-and-so many characters before or after such-
and-such a position. But how should this new position be
defined? Where should fseek(stream, 1000, SEEK_SET) land?


Ah, I see now. Far be it from me to inconvenience implementors... ;)
Thanks.


It's a bit more than that. How do you seek past a newline if you're
counting bytes?

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 13 '05 #7

"Christophe r Benson-Manica" <at***@nospam.c yberspace.org> wrote in message
news:bo******** **@chessie.cirr .com...
Eric Sosman <Er*********@su n.com> spoke thus:
Usually, people who wonder about this are thinking of
trying to fseek() to an arithmetically-calculated position
in the file: so-and-so many characters before or after such-
and-such a position. But how should this new position be
defined? Where should fseek(stream, 1000, SEEK_SET) land?


Ah, I see now. Far be it from me to inconvenience implementors... ;)


It is not so much the implementors convenience. The idea of fseek() is to
move the pointer without reading all the characters in between. C already
supplies ways to read all the characters, if you want to do that.

-- glen
Nov 13 '05 #8
Alan Balmer wrote:

On Mon, 10 Nov 2003 12:44:40 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.c yberspace.org> wrote:
Eric Sosman <Er*********@su n.com> spoke thus:
Usually, people who wonder about this are thinking of
trying to fseek() to an arithmetically-calculated position
in the file: so-and-so many characters before or after such-
and-such a position. But how should this new position be
defined? Where should fseek(stream, 1000, SEEK_SET) land?


Ah, I see now. Far be it from me to inconvenience implementors... ;)
Thanks.


It's a bit more than that. How do you seek past a newline if you're
counting bytes?

Is this a trick question? What does seeking have to do with counting
bytes? What does 'seeking past a newline' mean? If you are counting
bytes and discover a '\n' you might perform an ftell(stream) and save
the result in a long. This will be the address that you can eventually
pass to fseek() to arrive at the place you are now, one past the '\n'.
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #9

"Joe Wright" <jo********@ear thlink.net> wrote in message
news:3F******** ***@earthlink.n et...
Alan Balmer wrote:
On Mon, 10 Nov 2003 12:44:40 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.c yberspace.org> wrote:
Eric Sosman <Er*********@su n.com> spoke thus: Usually, people who wonder about this are thinking of
> trying to fseek() to an arithmetically-calculated position
> in the file: so-and-so many characters before or after such-
> and-such a position. But how should this new position be
> defined? Where should fseek(stream, 1000, SEEK_SET) land? Ah, I see now. Far be it from me to inconvenience implementors... ;)
Thanks.
It's a bit more than that. How do you seek past a newline if you're
counting bytes?

Is this a trick question? What does seeking have to do with counting
bytes? What does 'seeking past a newline' mean? If you are counting
bytes and discover a '\n' you might perform an ftell(stream) and save
the result in a long. This will be the address that you can eventually
pass to fseek() to arrive at the place you are now, one past the '\n'.


Seek, such as fseek(), puts the file position pointer at the specified
position in the file. In a file format with multiple line end characters,
all will be represented by a single '\n' to a program counting getc() calls
within a loop. If one expected the positions returned by ftell() or used
by fseek() to match those from counting getc() calls, the only
implementation for fseek() would be to read from the beginning of the file
while counting '\r' characters that are followed by '\n'.

The problem gets worse for the file format used by many IBM mainframe OSs.
The number of records in a block, or blocks on a disk track are not
necessarily knowable. In that case, pretty much the only solution is to
read every block from the
beginning, though it does not need to examine every byte that is read.
(For a file written on only once, it should be possible to seek by track and
block. If a file is closed, reopened, and then appended, there might be
short blocks in the middle of the file. There is no other way to account
for them.)

-- glen
Nov 13 '05 #10

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

Similar topics

15
16051
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
12863
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 }
2
3542
by: cedarson | last post by:
I am writing a program and have been instructeed to use the 'fseek', 'ftell', and 'stat' functions, however, after looking in the online manual for each of these, I am still unsure on how to use them. In my program, I am to write a code that opens a file, uses 'stat' to determine the file size, use 'fseek' to move the offset of the pointer, and finally use 'ftell' to obtain the file pointer index. Will someone please help? Again, thanks...
10
5949
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"...
3
2942
by: Chen ShuSheng | last post by:
HI, I am now study a segment of codes: ------------------------ printf("%p\t",fp); /*add by me*/ fseek(fp, 0L, SEEK_END); /* go to end of file */ printf("%p\t",fp); /*add by me*/ last = ftell(fp); cout<<"last="<<last<<"\t"; /*add by me*/ -------------------------
6
6142
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!
6
3735
by: ericunfuk | last post by:
I have posted a few other posts before this one today, I still can't solve my problem, I think I need to elaborate my problem now. I'm trying to send a file using UDP, below is a segment of my sender app, the sender and the receiver are both on the same machine and I have an Internet emulation gateway running on the same machine as well. What I'm confused about is I can't detect when the end of the file I'm sending has been reached,...
20
7520
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...
13
4486
by: thomas.mertes | last post by:
Hello Recently I discovered some problem. I have some C code which determines how many bytes are available in a file. Later I use this information to malloc a buffer of the correct size before I read the bytes. Determining the number of bytes available in a file is done in 5 steps: 1. Use tell(aFile) to get the current position.
0
8376
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
8815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8708
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8489
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
8594
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7307
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4149
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
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.