Read/Writing to the same file

T

Thomas Adam

Hi All,

I've been lurking on this list for a while, and I have a basic question
that I'm hoping someone can help answer.

I have read the FAQ regarding file I/O, and I am still having problems.
Basically, I'm opening a file like this:

myfile = File.open("/home/n6tadam/the_file", "r+")
arr[]

arr = myfile.readlines()
#change contents of arr

myfile.rewind()
myfile.print arr
myfile.close()

but still the file has not updated. Any ideas why? Also, when you want to
do in-place editing of the file, do you have to read the file into an
array first?

Hope that's not too confusing,

-- Thomas Adam


=====
Thomas Adam

"The Linux Weekend Mechanic" -- www.linuxgazette.com

________________________________________________________________________
Want to chat instantly with your online friends? Get the FREE Yahoo!
Messenger http://uk.messenger.yahoo.com/
 
M

mark

Hi All,

I've been lurking on this list for a while, and I have a basic question
that I'm hoping someone can help answer.

I have read the FAQ regarding file I/O, and I am still having problems.
Basically, I'm opening a file like this:
I can see two problems with the program
myfile = File.open("/home/n6tadam/the_file", "r+")
arr[]

This line causes an error since arr hasn't been initialised at this point
arr = myfile.readlines()
#change contents of arr

myfile.rewind()
myfile.print arr

This will put all the elements of arr on one line. If you want each element to
appear on a seperate line then you should use

myfile.puts arr
myfile.close()

aside from those two points the program works as expected.
but still the file has not updated. Any ideas why? Also, when you want to
do in-place editing of the file, do you have to read the file into an
array first?

Hope that's not too confusing,

-- Thomas Adam

Hope that helps

Best Regards

Mark Sparshatt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top