file position *tell()* works different

Richie Hindle richie at entrian.com
Fri Sep 19 12:35:15 EDT 2003


[Peter]
> I wonder if the function *tell()* is not correctly implemented under win32.

[Tim, quoting the standard]
> For a text stream, its file position indicator contains
> unspecified information, usable by the fseek function for returning
> the file position indicator for the stream to its position at the
> time of the ftell call

It still doesn't seem to work as specified:

------------------------------ peter.py ------------------------------

# Open the file in text mode, read a line, and store the position.
fp = file('test_data.txt', 'rt')
line = fp.readline()
storedPosition = fp.tell()
print 'Line: %r, file pointer after read: %d' % (line, storedPosition)

# Read some more and print it.
print 'Read another line from this position: %r' % fp.readline()

# Now seek back and read the same line again.
fp.seek(storedPosition)
print 'Another read from the same position: %r' % fp.readline()

----------------------------------------------------------------------

This prints:

Line: '0123456789\n', file pointer after read: 8
Read another line from this position: '0123456789\n'
Another read from the same position: '89\n'

I'd expect doing readline/tell/readline/seek/readline to read the same
line the second two times.  And however you implement tell and seek, a
tell value of 8 after reading 11 bytes looks pretty weird.

I'd write the same code in C if I had the time, so at least we could be
*sure* we can blame Microsoft.  8-)

-- 
Richie Hindle
richie at entrian.com>






More information about the Python-list mailing list