Alpesh Naik <naik.alp...@gmail.com> asked:
> I  am trying to edit the text file using perl script.
> i want to replace a particular text of file with new text.
> 
> but the problem is that the new text appeared at end of file;
> 
> How can i solve this problem,
> pls. help........

We can help you much better if we can actually see what you're doing, so please 
include sample code.

A working pattern for modifying a file goes like this:

1. open old file or reading

2. open temporary file for writing

3. read a line from the old file

4. if the line matches your criteria, then modify the line

5. write the line to the temporary file

6. If you have not reached the end of the old file yet, continue at 3.

7. close the old file

8. close the temporary file

9. rename the old file to a temporary filename

10. rename the temporary file to the name of the old file

11. optionally, delete the old file

It's not an in-place edit, but it works. If you need to do in-place edits, you 
should look at seek() and understand the constraints of doing an in-place edit.

HTH,
Thomas

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to