473,386 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

File read-write mode: problem appending after reading

Hi all,

Working with read and write operations on a file I stumbled on a
complication when writes fail following a read to the end.
>>f = file ('T:/z', 'r+b')
f.write ('abcdefg')
f.tell ()
30L
>>f.seek (0)
f.read ()
'abcdefg'
>>f.flush () # Calling or not makes no difference
f.write ('abcdefg')
Traceback (most recent call last):
File "<pyshell#62>", line 1, in -toplevel-
f.write ('abcdefg')
IOError: (0, 'Error')

Flushing doesn't help. I found two work arounds:
>>f.read ()
'abcdefg'
>>f.read () # Workaround 1: A second read (returning an empty string)
''
>>f.write ('abcdefg')
(No error)
>>f.read ()
'abcdefg'
>>f.seek (f.tell ()) # Workaround 2: Setting the cursor (to where
it is!)
>>f.write ('abcdefg')
(No error)

I found no problem with writing into the file. So it looks like it has
to do with the cursor which a read puts past the end, unless it is past
the end, in which case it goes back to the end. Is there a less kludgy
alternative to "fseek (ftell ())"?

Frederic

Oct 13 '06 #1
0 1035

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

Similar topics

10
by: Yang Li Ke | last post by:
Hi guys! I have some datas that I must check everytime a visitor comes to my site What is better to do: 1- Read data from a file or 2- Read data from a mysql db Thank you
6
by: Russell E. Owen | last post by:
At one time, mixing for x in file and readline was dangerous. For example: for line in file: # read some lines from a file, then break nextline = readline() # bad would not do what a naive...
3
by: Tony Murphy | last post by:
Strange problem, I read a file into a string using ifstream, ostringstream and string and end part of the file is missing (file size ~9.5kb, ~9k read). its a html file. using windows nt 4 ...
0
by: SeanR | last post by:
I have a function to copare two files. It will first copy the original file form a different server to a local temp path and then compare that version to a version that has been restored form tape....
15
by: SK | last post by:
Hey folks, I am searching for a string (say "ABC") backwards in a file. First I seek to the end. Then I try to make a check like - do { file.clear (); file.get(c); file.seekg(-2,...
4
by: yo_mismo | last post by:
Hi everyone, I'm trying to read the first line of a file this way: .... .... .... .... new_line=0; while((read=read(fd, &info, sizeof(info))) > 0 && !new_line){ if (strcmp(&info, "\n") !=...
4
by: John please don't spam me! | last post by:
VB.Net 2003 Hi, 2 questions: 1. I want to read a file in without locking it, as it is Log file. 2. I want to be able to read from the last point it wa read upto. The project is: Search...
5
by: lovecreatesbea... | last post by:
The condition at line 31 is added to check if the program finished to read the whole file. Is it needed and correct? Thank you. #include <fstream> #include <iostream> #include <string> using...
4
by: mpatharkar | last post by:
Hi all, I wrote a script to search a pattern in input file and if pattern does not found in input file ,print that pattern in to output file. The input file is...
15
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...

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.