Re: Text mode fseek/ftell



On Fri, 31 Mar 2006 10:21:47 -0500, Kenneth Brody
<kenbrody@xxxxxxxxxxx> wrote in comp.lang.c:

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" behavior? Or is it still "defined"?

The problem comes in how ftell() reports the current position. (And,
subsequently fseek()ing back to the same position is wrong.)

Suppose that you have fread() the following 12 characters, starting at
the beginning of the file:

'1' '2' '3' '4' '5' 0x0a '1' '2' '3' '4' '5' 0x0a

(Remember, this file is in Unix format, with a single 0x0a for end-of-
line.)

While you are now at offset 12 within the file, ftell() will return 14,
because it assumes that those '\n' newlines are really CR+LF, and that
the CR was stripped off when read. (Had this file been in Windows format,
you really would be at offset 14 after reading those 12 characters.) For
each 0x0a returned by fread(), ftell() will assume you have advanced two
characters in the file.

The net result here is that a subsequent fseek() to the same position
will be wrong.


So, have I invoked undefined behavior by reading a Unix text file in a
Windows environment? Or is the compiler allowed to return the "wrong"
value as part of an "implementation defined" restriction? Or is this
a bug in the compiler's runtime library?

In addition to Ben's pointing out correctly issues about fseek() and
ftell() limitations, you left out one piece of important information,
namely did you open the file in text or binary mode?

If you open a file in text mode, and it does not actually contain the
format for text files on your platform, you are lying to your compiler
and its library functions. If you lie to your compiler, it will get
its revenge.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
.



Relevant Pages

  • Re: client -server interaction over XML supporting multiple protocols
    ... > analyzing various models to develop the server. ... The Windows WinInet at just the socket layer is also very ... expected to be different for each platform. ... will the XML size constraints significantly hinder the request ...
    (comp.lang.cpp)
  • Re: It is almost certain now, INTEL will have 64bit x86 !!
    ... >>IA64 boxes, and then support them on the 8086, on to of the original ... Its difficult to see what future the Windows on IPF port has ... Windows on Itanium was always more of a marketing exercise allowing ... But the introduction of Windows on AMD64 and Intels new platform ...
    (comp.os.vms)
  • Re: Checking for Modification to a Set of Files
    ... use .NET on anything other than Windows for production use. ... production applications, but I may have missed your point. ... platform for anything but a Windows system. ...
    (comp.programming)
  • RE: HP announces new Integrity servers
    ... Customers move to platforms like Windows, ... Every platform has strengths and weakneses and challenges. ... IBM's X3 systems may actually support 128-core configurations ...
    (comp.os.vms)
  • Re: Borlands linux strategy ?
    ... Sorry, it's the general mentality of Borland, and reinforced by users who ... believe that Delphi should just remain a windows platform tool. ...
    (borland.public.delphi.non-technical)