Re: Fast editing of text file? [message #44448] |
Sun, 19 June 2005 01:12  |
R.Bauer
Messages: 1424 Registered: November 1998
|
Senior Member |
|
|
t_314159@yahoo.com wrote:
Hi, nameless
does it really save time to proceed this way. On which timescale we are
talking?
If I understand right I would read in the whole file at once in a string
array. You could determine the file line length by file_lines(). Then I
would keep this array in memory. I would alter index 11 and then I would
write the whole file at once.
This needs only seconds.
PRO example
file='test.dat'
txt=MAKE_ARRAY(file_lines(file),/STRING)
OPENR,lun,file,/get_lun
READF,lun,txt
FREE_LUN,lun
a=systime(1)
FOR I=0L,40000L DO BEGIN
txt[11]=STRTRIM(STRING(i),2)
OPENW,lun,file,/GET_LUN,width=200
PRINTF,lun,txt
FREE_LUN,lun
ENDFOR
print,systime(1)-a
END
> 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!!!
--
Forschungszentrum Juelich
email: R.Bauer@fz-juelich.de
http://www.fz-juelich.de/icg/icg-i/
============================================================ ======
a IDL library at ForschungsZentrum Juelich
http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_lib_intro. html
|
|
|