473,883 Members | 2,621 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 6279

"Alan Balmer" <al******@att.n et> wrote in message
news:bd******** *************** *********@4ax.c om...
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 toa 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.


Newline conversion is one of the common changes, but not the only one.

The PDP-10 (TOPS-10 and TOPS-20 OS) store text files as five 7 bit ASCII
characters per word. A possible binary format is four 9 bit char's per
word. The results will be very different if the wrong one is used.

CDC had a series of machines with 60 bit words, which used either 6 or 12
bits per character, depending on the bit patterns. (Similar to UTF-8
coding, where some bits indicate the length.) I don't know what CHAR_BIT
might be, though. In this case, with variable length characters, or for
that matter with UTF-8, one can imagine that text streams and binary
streams would work differently.
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 IBM's mainframe OSs lines never have newline characters on them, though
they could contain newline characters.

There could be systems that keep text and binary files completely separate,
such that no operations are allowed on the wrong type. I don't know of any,
though.
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.
I have heard about people trying to make executable text files on various
systems. That is, only opcodes that are printable characters are allowed.
Very strange, but some systems allow it.
Perhaps Dan is right, and the writers made a mistake. I'm in no
position to make a judgment on that.


The standard tries to allow for reasonable differences in hardware, OS, and
file system design. Many features are to accommodate features of existing
systems, or ones that existed in the past.

-- glen
Nov 13 '05 #41
In <p7ysb.134611$2 75.397546@attbi _s53> "Glen Herrmannsfeldt" <ga*@ugcs.calte ch.edu> writes:

"Dan Pop" <Da*****@cern.c h> wrote in message
news:bo******* ***@sunnews.cer n.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).


There is nothing preventing the system's normal text editor(s) from being
written in C ;-) It's even current practice, these days.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #42
In <lp************ *************** *****@4ax.com> Alan Balmer <al******@att.n et> writes:
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.

^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^
When you have to resort to Trollsdale favourite "argument", there *is*
something wrong with your position in the debate.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #43
In <XPEsb.134993$m Z5.932687@attbi _s54> "Glen Herrmannsfeldt" <ga*@ugcs.calte ch.edu> writes:

The standard tries to allow for reasonable differences in hardware, OS, and
file system design. Many features are to accommodate features of existing
systems, or ones that existed in the past.


Then again, we have the DeathStation. Text files *must* have the .t
extension (otherwise fopen in text mode fails) and binary files *must*
have the .b extension (otherwise fopen in binary mode fails). Files with
other extensions cannot be opened. Neither rename() nor any system
utility allows changing the file's extension. Perfectly allowed by the
standard.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #44
On Thu, 13 Nov 2003 00:34:22 +0100, Irrwahn Grausewitz
<ir*******@free net.de> wrote:
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


Like you, I don't know that it's pertinent to the current discussion,
but it is interesting, and rather oddly tucked in to the section. An
afterthought to cover some actual implementation?

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 13 '05 #45
On Thu, 13 Nov 2003 05:41:11 GMT, "Glen Herrmannsfeldt"
<ga*@ugcs.calte ch.edu> wrote:

"Alan Balmer" <al******@att.n et> wrote in message
news:bd******* *************** **********@4ax. com...
On Wed, 12 Nov 2003 23:13:01 GMT, la************@ eds.com wrote:
>Alan Balmer <al******@att.n et> wrote [quoting Dan Pop]:
>>
<snip> 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.
Newline conversion is one of the common changes, but not the only one.

The PDP-10 (TOPS-10 and TOPS-20 OS) store text files as five 7 bit ASCII
characters per word. A possible binary format is four 9 bit char's per
word. The results will be very different if the wrong one is used.


It really doesn't matter -the standard refers to streams, not the
physical implementation. Both binary and text streams are a sequence
of characters as far as the standard is concerned. You ask for a
character, the system must do whatever is necessary to provide one.
CDC had a series of machines with 60 bit words, which used either 6 or 12
bits per character, depending on the bit patterns. (Similar to UTF-8
coding, where some bits indicate the length.) I don't know what CHAR_BIT
might be, though. In this case, with variable length characters, or for
that matter with UTF-8, one can imagine that text streams and binary
streams would work differently.
I wrote programs to convert tapes from those 60-bit CDC machines :-)
That wasn't the only strange thing - as I recall they would split data
at the end of a record.

However, I don't think a C implementation would provide variable
length characters, in spite of the hardware.
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 IBM's mainframe OSs lines never have newline characters on them, though
they could contain newline characters.


Again, you appear to be confusing bit patterns on a disk with a C
stream. If there's a text stream, and it has more than one line, each
line must end with a newline.
There could be systems that keep text and binary files completely separate,
such that no operations are allowed on the wrong type. I don't know of any,
though.
For good reason. Think about it.

What "operations " would you prohibit for each type of file?
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.
I have heard about people trying to make executable text files on various
systems. That is, only opcodes that are printable characters are allowed.
Very strange, but some systems allow it.


Unix scripts are executable text files ;-)

You may be thinking about certain loader formats which use only
printable characters.


--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 13 '05 #46
On 13 Nov 2003 13:33:51 GMT, Da*****@cern.ch (Dan Pop) wrote:
In <lp************ *************** *****@4ax.com> Alan Balmer <al******@att.n et> writes:
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.

^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^
When you have to resort to Trollsdale favourite "argument", there *is*
something wrong with your position in the debate.

Dan


Incorrect premise - I did not "have to resort" to anything. Striking
the last line does not affect the truth of the posting.

OTOH, when you have to resort to this type of comment, I am forced to
conclude that you have no further useful input.

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 13 '05 #47
Alan Balmer wrote:

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.


Unix. fopen(".", "wb") will fail, even though the
file "." exists and even if it is writeable by the user
running the program.

--
Er*********@sun .com
Nov 13 '05 #48
On Thu, 13 Nov 2003 11:45:41 -0500, Eric Sosman <Er*********@su n.com>
wrote:
Alan Balmer wrote:

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.


Unix. fopen(".", "wb") will fail, even though the
file "." exists and even if it is writeable by the user
running the program.


And fopen(".", "w") will succeed? I've never tried this (since "." is
the current directory in Unix, I've never had a need.) What's the
error code, if you know off-hand? Is this on all Unices? (The
questions are obviously off-topic for c.l.c, I'm just trying to
minimize my test time.)

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

"Dan Pop" <Da*****@cern.c h> wrote in message
news:bp******** **@sunnews.cern .ch...
In <p7ysb.134611$2 75.397546@attbi _s53> "Glen Herrmannsfeldt"

<ga*@ugcs.calte ch.edu> writes:
"Dan Pop" <Da*****@cern.c h> wrote in message
news:bo******* ***@sunnews.cer n.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 readinga C text file would not read text files commonly available on a system, suchas produced by the systems normal text editor(s).


There is nothing preventing the system's normal text editor(s) from being
written in C ;-) It's even current practice, these days.


In that case, there shouldn't be any problems reading the files. In many
older systems, though, that wasn't true. Continuing my previous comments, I
don't believe that the TOPS-10 editors were written in C. It would be
unfortunate for a C implementation for TOPS-10 not to read the text files
written by other programs, even though it could follow a C standard exactly.

-- glen
Nov 13 '05 #50

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

Similar topics

15
16252
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
13054
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
5988
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
2962
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
6168
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
7563
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
9933
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
11125
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
10734
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
10407
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
5794
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
5982
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4607
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
4211
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3230
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.