X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 27 Oct 2008 12:40 PM by  anon
how to overrite a line in a text file?
 1 Replies
Sort:
You are not authorized to post a reply.
Author Messages

anon



New Member


Posts:
New Member


--
27 Oct 2008 12:40 PM
    hi, I am trying to overrite a line in a text file. Currently in my program, I am search for the string (or line) that I want to replace. I have a line in the file: 'data file :='. I want to replace it with 'data file := image". I am using [printf] command and that inserts a line (does not replace it). If anyone can help, that will be appreciated. Thanks,  

    Deleted User



    New Member


    Posts:33
    New Member


    --
    01 Dec 2008 03:18 PM
    If each line of text can be variable length, then you cannot just overwrite it. I would suggest the following. Use file_copy to copy the original file to a temporary file. Open the temporary file. Open an output file with the name of the original file. Read the temporary file; if the line is not the line you want to change, then write it to the new output file; if the line is the line you want to change (data file:= ) then write the new line to the output file. Close the temporary file and delete it if it is un-needed. Close the output file. It is brute force, but it works. If lines are fixed lengths (always, say, 1000 characters and always blank padded) then you would know exactly which bytes to overwrite, and you could always go to the correct part of the text file; you need to be careful of the "end of line characters" of which there can be either one or two, depending on the OS.      
    You are not authorized to post a reply.