473,836 Members | 1,510 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
62 6267
On 11 Nov 2003 16:45:32 GMT, Da*****@cern.ch (Dan Pop) wrote:
What does 'seeking past a newline' mean?


The number of bytes in a newline is platform dependent.


Entirely irrelevant, for both kind of streams. If you attach a binary
stream to a text file, all the bets are off: you may not see a single
newline character in the whole file (implementation s storing each line
of text in a variable size record typically don't bother to store the
newline character at all: it is implied, after the last character of the
record).

Which was exactly the point. There's more involved in seeking a text
stream than simply counting characters. In other words, the OP's
implication that the different treatment of text streams in the
standard is simply an "inconvenie nce" to implementors is incorrect.

Not irrelevant at all

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 13 '05 #21
On Tue, 11 Nov 2003 20:37:45 +0100, Irrwahn Grausewitz
<ir*******@free net.de> wrote:
> For a text stream, either offset shall be zero, or offset shall be a
> value returned by an earlier successful call to the ftell function on
> a stream associated with the same file and whence shall be SEEK_SET.
>
> Well, it says not explicitly: "a stream associated with the same file
> opened in the same (text) mode", but the C-Rationale explains:
Quite right. It does not say that. If the writers had wanted to say
that, I suspect they would have. >
> "Whereas a binary file can be treated as an ordered sequence of bytes
> counting from zero, a text file need not map one-to-one to its
> internal representation (see 7.19.2). Thus, only seeks to an earlier
> reported position are permitted for text files. [...]"
And neither does this. However, this does say explicitly what I was
trying to convey to the OP. Thank you. >
> Therefore I don't think the procedure you suggested will work
> portably.

Depends on what you mean by "work." I would expect it to "work" on
any implementation. I wouldn't, however, try to predict the
relationship between a given offset in a binary file and the same
offset in a text file.

I don't have a long list of applications for the procedure, either,
but I don't see that it's prohibited by the standard.

Remember that the difference between text files and binary files (if
any) is a characteristic of the implementation. Text files can always
be treated as binary files, but the reverse is not generally true, and
even if you know it's a text file, interpreting it as text from a
binary stream is not portable.

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 13 '05 #22
Alan Balmer <al******@att.n et> wrote:
On Tue, 11 Nov 2003 20:37:45 +0100, Irrwahn Grausewitz <ir*******@free net.de> wrote:
For a text stream, either offset shall be zero, or offset shall be a
> value returned by an earlier successful call to the ftell function on
> a stream associated with the same file and whence shall be SEEK_SET.
>
> Well, it says not explicitly: "a stream associated with the same file
> opened in the same (text) mode", but the C-Rationale explains:
Quite right. It does not say that. If the writers had wanted to say
that, I suspect they would have.
Maybe someone would like to generate a DR about this.
> "Whereas a binary file can be treated as an ordered sequence of bytes
> counting from zero, a text file need not map one-to-one to its
> internal representation (see 7.19.2). Thus, only seeks to an earlier
> reported position are permitted for text files. [...]"
And neither does this.


Not explicitly, but IMHO implicitly. Otherwise the "Thus, ..." part
makes no sense. However, it's quoted from the Rationale, not the
Standard.
However, this does say explicitly what I was
trying to convey to the OP. Thank you.

> Therefore I don't think the procedure you suggested will work
> portably.

Depends on what you mean by "work." I would expect it to "work" on
any implementation. I wouldn't, however, try to predict the
relationship between a given offset in a binary file and the same
offset in a text file.


That's an, err, interesting definition of "work". Anyway, if you are
aware of these facts, why did you suggest upthread:

AB> However, you can open the same file as binary, then use the
AB> results of fseek and ftell to position the text file.

as if this procedure would do anything useful?

IOW: you at least forgot to add a disclaimer. ;-)

Regards
--
Irrwahn
(ir*******@free net.de)
Nov 13 '05 #23
On Wed, 12 Nov 2003 01:16:36 +0100, Irrwahn Grausewitz
<ir*******@free net.de> wrote:
Depends on what you mean by "work." I would expect it to "work" on
any implementation. I wouldn't, however, try to predict the
relationship between a given offset in a binary file and the same
offset in a text file.
That's an, err, interesting definition of "work".


What's yours?
You can write the code, any compiler should compile it, and you can
execute it. It will work, whether the results are useful or not. I
wish that I could claim that all the work I've ever done turned out to
be useful ;-)
Anyway, if you are
aware of these facts, why did you suggest upthread:

AB> However, you can open the same file as binary, then use the
AB> results of fseek and ftell to position the text file.

as if this procedure would do anything useful?

IOW: you at least forgot to add a disclaimer. ;-)


It wasn't a suggestion, but a comment. After all, the OP wasn't
looking for suggestions, but commenting on how he thought things
*should* work, as opposed to how they *do* work.

In the course of the thread, someone brought up the fact that offsets
are counted as characters for binary files, but nothing in particular
for text files. While commenting on that, I mentioned that a file can
be opened and positioned as binary, and that the standard apparently
allows that position to be used in fseek for the same file opened as
text. I find that interesting, and could probably even invent a
legitimate use for it (investigating the actual structure of a text
file, perhaps.)

I suggest that you reread the thread from the beginning, remembering
that my prose generation is not always the best, and I may sometimes
mistakenly assume that the reader is thinking in the same twisted
direction that I am :-)

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 13 '05 #24
Alan Balmer <al******@att.n et> wrote:

<snip>
I suggest that you reread the thread from the beginning, remembering
that my prose generation is not always the best, and I may sometimes
mistakenly assume that the reader is thinking in the same twisted
direction that I am :-)


The root of misinterpretati on that may be, yes :-)
--
Irrwahn
(ir*******@free net.de)
Nov 13 '05 #25
In <35************ *************** *****@4ax.com> Alan Balmer <al******@att.n et> writes:
On 11 Nov 2003 17:02:25 GMT, Da*****@cern.ch (Dan Pop) wrote:
In <b2************ *************** *****@4ax.com> Alan Balmer <al******@att.n et> writes:
On Tue, 11 Nov 2003 13:42:50 GMT, rl*@hoekstra-uitgeverij.nl (Richard
Bos) wrote:

Alan Balmer <al******@att.n et> wrote:

> On Tue, 11 Nov 2003 00:24:47 GMT, Joe Wright
> <jo********@ear thlink.net> wrote:
>
> >> 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?
>
> The offset parameter of fseek is in bytes.

Only for a binary stream.

True - the standard does not define the units for text streams (though
it's still bytes in at least some implementations .) However, you can
open the same file as binary, then use the results of fseek and ftell
to position the text file.


Where did you get the idea from? Chapter and verse, please.

Dan


7.19.9.2

"For a text stream, either offset shall be zero, or offset shall be a
value returned by an _earlier successful call to the ftell function on
a stream associated with the same file_ and whence shall be SEEK_SET."

Emphasis added.


The other stream *must* be a text stream too. Connect a binary stream to
a text file and all the bets are off.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #26
In <vg************ *************** *****@4ax.com> Alan Balmer <al******@att.n et> writes:
On 11 Nov 2003 16:45:32 GMT, Da*****@cern.ch (Dan Pop) wrote:
What does 'seeking past a newline' mean?

The number of bytes in a newline is platform dependent.


Entirely irrelevant, for both kind of streams. If you attach a binary
stream to a text file, all the bets are off: you may not see a single
newline character in the whole file (implementation s storing each line
of text in a variable size record typically don't bother to store the
newline character at all: it is implied, after the last character of the
record).

Which was exactly the point. There's more involved in seeking a text
stream than simply counting characters.


And yet, you claim that you can use character offsets as arguments to a
fseek call on a text stream.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #27
On 12 Nov 2003 13:25:17 GMT, Da*****@cern.ch (Dan Pop) wrote:
7.19.9.2

"For a text stream, either offset shall be zero, or offset shall be a
value returned by an _earlier successful call to the ftell function on
a stream associated with the same file_ and whence shall be SEEK_SET."

Emphasis added.


The other stream *must* be a text stream too. Connect a binary stream to
a text file and all the bets are off.


Chapter and verse, please.

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 13 '05 #28
On 12 Nov 2003 13:26:55 GMT, Da*****@cern.ch (Dan Pop) wrote:
Which was exactly the point. There's more involved in seeking a text
stream than simply counting characters.


And yet, you claim that you can use character offsets as arguments to a
fseek call on a text stream.


Of course you can. You can also use random numbers. I don't claim to
be able to portably relate the results to anything in particular.

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 13 '05 #29
In <d8************ *************** *****@4ax.com> Alan Balmer <al******@att.n et> writes:
On 12 Nov 2003 13:26:55 GMT, Da*****@cern.ch (Dan Pop) wrote:
Which was exactly the point. There's more involved in seeking a text
stream than simply counting characters.
And yet, you claim that you can use character offsets as arguments to a
fseek call on a text stream.


Of course you can. You can also use random numbers.


The idea was to be able to seek to well defined positions inside the file.
I don't claim to
be able to portably relate the results to anything in particular.


In general, the results of ftell on a binary stream are
useless/meaningless to any text stream on the same implementation.

The thing you don't get is that the encoding of the file position returned
by ftell on a text stream need not be a plain byte offset. It could be
a record number and a byte offset inside the record.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #30

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

Similar topics

15
16209
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
13021
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
3562
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
5986
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
2960
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
6165
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
3755
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
7556
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
4502
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
9814
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
9666
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,...
0
10838
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
10250
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
9369
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...
1
7788
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5645
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...
1
4447
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
3
3111
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.