473,890 Members | 1,339 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 6283
In <l6************ *************** *****@4ax.com> Alan Balmer <al******@att.n et> writes:
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.


It's an obvious bug in the standard. Ask in comp.std.c if you don't
believe me.

Binary files and text files can have completely different internal
representations and the standard doesn't provide any guarantee about
what happens when you open a text file in binary mode or vice versa.
It only addresses the cases when a binary stream is attached to a binary
file and a text stream to a text file.

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

Binary files and text files can have completely different internal
representations and the standard doesn't provide any guarantee about
what happens when you open a text file in binary mode or vice versa.
It only addresses the cases when a binary stream is attached to a binary
file and a text stream to a text file.


I don't think the notions of "text file" and "binary
file" as disjoint categories will withstand scrutiny. At
least, I've never seen any attempted definitions that were
beyond reproach.

The Standard says very little about "text files" and
"binary files," and that seems a wise choice. About all
we can infer is that a "text file" is an entity suitable
for access via a text stream, while a "binary file" is
suited to binary streams. Some kinds of files may work
with both kinds of streams, and some kinds of files may
work with neither -- and the implementation' s whim rules
the day.

--
Er*********@sun .com
Nov 13 '05 #32
On 12 Nov 2003 17:27:25 GMT, Da*****@cern.ch (Dan Pop) wrote:
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

Sorry to dispell your illusion of omniscience (do you fancy yourself a
telepath?), but you have no idea what I "get" and "don't get." I not
only "get" that possibility, I have designed storage systems using
similar methodology. What *you* don't "get" (or at least want to argue
about) is that any file may be opened in binary mode, and a character
offset generated by fseek/ftell. The standard does not preclude using
that offset in fseek on a text file. Obviously, as I stated
previously, the standard says nothing about the usefulness of doing
this. You say this is an "obvious bug" in the standard. I suggest you
submit it for correction.

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 13 '05 #33
In <3F************ ***@sun.com> Eric Sosman <Er*********@su n.com> writes:
Dan Pop wrote:

Binary files and text files can have completely different internal
representations and the standard doesn't provide any guarantee about
what happens when you open a text file in binary mode or vice versa.
It only addresses the cases when a binary stream is attached to a binary
file and a text stream to a text file.


I don't think the notions of "text file" and "binary
file" as disjoint categories will withstand scrutiny. At
least, I've never seen any attempted definitions that were
beyond reproach.


In the context of the C programming language, a text file is a file
created via a text stream and a binary file is a file created via a
binary stream. No guarantees for files created by other means than
correct C programs.

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

"Dan Pop" <Da*****@cern.c h> wrote in message
news:bo******** **@sunnews.cern .ch...
In <3F************ ***@sun.com> Eric Sosman <Er*********@su n.com> writes:


(snip)
I don't think the notions of "text file" and "binary
file" as disjoint categories will withstand scrutiny. At
least, I've never seen any attempted definitions that were
beyond reproach.


In the context of the C programming language, a text file is a file
created via a text stream and a binary file is a file created via a
binary stream. No guarantees for files created by other means than
correct C programs.


That may be true, but I would be disappointed to find a system where reading
a C text file would not read text files commonly available on a system, such
as produced by the systems normal text editor(s).

For binary files, though, one may find that the only way to create a binary
file that C programs can read is one written by a C program. In many
languages, this is normal for binary files. In C it is often expected that
one can read any binary file, written by any program in any language, though
that may not be true on all systems.

Any system that normally stores text files using less than eight bits per
character would make it hard for text and binary files to be compatible.

-- glen
Nov 13 '05 #35
Alan Balmer <al******@att.n et> wrote:

What *you* don't "get" (or at least want to argue
about) is that any file may be opened in binary mode


That is true on many systems, but it is not guaranteed by the standard.

-Larry Jones

At times like these, all Mom can think of is how long she was in
labor with me. -- Calvin
Nov 13 '05 #36
Alan Balmer <al******@att.n et> wrote [quoting Dan Pop]:
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.


See 7.19.5.3 (fopen) -- the standard does not provide any way to connect
a text stream to a binary file or vice versa. Attempting to open a
binary file in text mode or vice versa results in undefined behavior.

-Larry Jones

In my opinion, we don't devote nearly enough scientific research
to finding a cure for jerks. -- Calvin
Nov 13 '05 #37
la************@ eds.com wrote:
Alan Balmer <al******@att.n et> wrote [quoting Dan Pop]:
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.


See 7.19.5.3 (fopen) -- the standard does not provide any way to connect
a text stream to a binary file or vice versa. Attempting to open a
binary file in text mode or vice versa results in undefined behavior.


Seems to be the appropriate section, yes; but re-reading it I
stumbled over something I've overseen till now:

7.19.5.3p6
[...]
Opening (or creating) a text file with update mode may instead
open (or create) a binary stream in some implementations .

I'm not sure if this can be used to form an argument in the debate
about the relationship of binary and text files, and if so, pro or
contra which side; I'm just puzzled that such a strange behaviour
is sanctioned by the standard...

Regards
--
Irrwahn
(ir*******@free net.de)
Nov 13 '05 #38
On Wed, 12 Nov 2003 23:13:01 GMT, la************@ eds.com wrote:
Alan Balmer <al******@att.n et> wrote [quoting Dan Pop]:
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.


See 7.19.5.3 (fopen) -- the standard does not provide any way to connect
a text stream to a binary file or vice versa. Attempting to open a
binary file in text mode or vice versa results in undefined behavior.

This paragraph only describes the mode that the stream is opened in,
saying nothing about the files themselves. It does not say that
opening a file in the "wrong" mode results in undefined behavior. The
mode is important to the user, since in text mode it tells the I/O
implementation that it must look for, and possibly map, newline
characters. In binary mode, newline characters are treated the same as
any other character.

Opening a text file in binary mode is perfectly legitimate - in fact
the standard provides no way to distinguish between a binary file and
a text file. Refer to 17.19.2, where the two types of streams are
defined. Now, consider a "text" file containing one "line." The thing
that makes it a text file is that each "line" has a terminating
newline character. But the standard says that the last line need not
have the terminating newline (it's implementation dependent.) How can
this file be distinguished from a binary file? What will the
implementation do to me if I open it in binary mode?

On the other hand, presume that I have a binary file, say an
executable program. This file may contain numerous instances of a
character with the value 0xA, which happens to be the newline
character on the system I'm using now. Does that make it a text file?
Obviously not, but it may meet all the criteria for opening as a text
stream.

Perhaps Dan is right, and the writers made a mistake. I'm in no
position to make a judgment on that.

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 13 '05 #39
On Wed, 12 Nov 2003 23:13:01 GMT, la************@ eds.com wrote:
Alan Balmer <al******@att.n et> wrote:

What *you* don't "get" (or at least want to argue
about) is that any file may be opened in binary mode


That is true on many systems, but it is not guaranteed by the standard.

Nor is it prohibited by the standard.

Name me a system where it can't be done.

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

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

Similar topics

15
16265
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
13071
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
3566
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
5989
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
2963
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
6169
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
3758
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
7566
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
4504
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
9826
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
11236
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
10830
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...
0
10468
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
9641
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
8018
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...
1
4682
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
4276
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3283
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.