Re: Fast editing of text file? [message #44492 is a reply to message #44490] |
Tue, 14 June 2005 16:26  |
Michael Wallace
Messages: 409 Registered: December 2003
|
Senior Member |
|
|
You can divide your file into two files: file with everything except the
12th line, file with only the 12th line. When you need to update the
12th line, you won't have to recreate the original file. You'll only
need to update the little file with the 12th line in it. That should be
quite a bit faster than having to recreate the original file each time.
-Mike
t_314159@yahoo.com wrote:
> Hi,
> I have a text file that I must access many times during program
> execution (1000's of times, really!) and I *always* only need to change
> the the text on the 12th line of the file. My question, how to
> efficiently access and alter the text of ONLY the 12th line without
> altering even a single space or comma on any of the rest of the lines?
> And did I mention fast since i've got to loop through this many times?
>
> For example:
>
> --BEGIN FILE---
> test file
> A
> 0 1 0
> 4,5,1
> [some text lines I'm not showing here, I'll show my 12th line next]
> 500, 0.02, 1.00587
> [more text lines to the end of file]
> --END FILE---
>
> What I need to do is keep the file exactly as is (there's some weird
> formatting of spaces, tabs, commas on different lines that a follow-on
> old fortran program expects) EXCEPT for the 12th line where I need to
> change those 3 #'s every time (I know that line is expected to be
> comma+space delimeted).
>
> The only thing I can think of is to:
> openr, infile, inFileName, /get_lun;
> create a new outfile for write;
> loop through infile line by line writing directly to outfile up to, but
> not including, the 12th line;
> do a newline = strtrim(a,2) + ", " + strtrim(b,2) + ", " + strtrim(c,2)
> where a, b, and c are int,float,or double values I need to write on
> line 12;
> writeu, outfile, newline
> continue looping through infile writing lines 13 to EOF to outfile;
> free_lun both infile and outfile;
> delete the infile;
> rename the outfile to the infile's old name;
> call the model that works on that text file;
> repeat 40,000 times with new values for a, b, c each time;
>
> Is there a better way? Note that significant digits of a, b, c may
> change each time so the formatting and # of places each takes will vary
> each time. Next time the 3 numbers may be .3, 1000.00003, 0.
>
> Thanks you for the help!!!
>
|
|
|