You need to open the file, save the contents in memory, then close the file. Now re-open the file for writing and print the saved contents to it making the changes as needed. This is fine for non crictical files that are small (once you open the file for write, it's contents are deleted so if your script crashes you lose the file completely).
For more safety and for bigger files, open the file and create a new file. Write the contents of the old file to the new file one line at a time, making the changes as needed. When that has finished, rename the old file to oldfile.bak, then rename the new file to whatever the old one was called. HTH John -----Original Message----- From: Ahmed Moustafa [mailto:[EMAIL PROTECTED]] Sent: 02 January 2002 02:31 To: [EMAIL PROTECTED]; Leon Cc: [EMAIL PROTECTED] Subject: Re: add text to the end of a word. HELP!! Leon, I've tested reading and writing from the same file and it didn't work. I think that can't be done in any language. Leon wrote: > ----- Original Message ----- > From: "Ahmed Moustafa" <[EMAIL PROTECTED]> > Newsgroups: perl.beginners > To: "Leon" <[EMAIL PROTECTED]> > Cc: <[EMAIL PROTECTED]> > >>Can you open a file for reading and writing as the same time? >> > > I have asked the question of can I read and write to the same file > simultaneously, such as this :- > open FILE, 'myfile.txt' or die "$!\n"; > open FILE2, '>myfile.txt' or die "$!\n"; > while (<FILE>){ > # do some changes to $_; > print FILE2 $_; > }; > > but I did not get any response. Therefore I dont think we can do that. > > ---- end of msg ----- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --------------------------Confidentiality--------------------------. This E-mail is confidential. It should not be read, copied, disclosed or used by any person other than the intended recipient. Unauthorised use, disclosure or copying by whatever medium is strictly prohibited and may be unlawful. If you have received this E-mail in error please contact the sender immediately and delete the E-mail from your system. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]