comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: Write to a certain line of a file
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: Write to a certain line of a file [message #78956] Thu, 19 January 2012 11:02 Go to previous message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Michael Galloy writes:

> You are essentially treating the file as a binary file where you have
> more options, but also more responsibility for keeping things straight.
>
> Sure, this would work in the case where the new output has the exact
> same length in bytes as the old output, but if not everything is going
> to get screwed up. You could rewrite the file after the point of
> insertion to fix this, but how much is it worth it to get around writing
> this file?

I was curious how long it would take to re-write a 10000 line ASCII file
after replacing a line in it. Answer: 0.014 seconds!

Here is the code I used.

;----------------------------------------------------------- ----
; Create a random set of strings.
LENGTH=10000
len = Round(Scale_Vector(Randomu(-3L, LENGTH), 10, 60))
file = StrArr(LENGTH)
FOR j=0,N_Elements(len)-1 DO BEGIN
str = Byte(Scale_Vector(RandomU(seed, len[j]), 97, 122))
space = Fix(randomu(seed, len[j]/5 > 1) * len[j] > 2)
str[space] = 32B
file[j] = String(str)
ENDFOR

; Write the random strings to a file.
OpenW, lun, 'test_ascii_file.txt', /Get_Lun
FOR j=0, LENGTH-1 DO PrintF, lun, file[j]
Free_Lun, lun

; Replace line 50 with 'The dog ate my homework!'
start = Systime(1)
lines = File_Lines('test_ascii_file.txt')
OpenR, lun, 'test_ascii_file.txt', /Get_Lun
strings = StrArr(lines)
strings[49] = 'The dog ate my homework!'
Free_Lun, lun

OpenW, lun, 'test_ascii_file_1.txt'
FOR j=0,N_Elements(strings)-1 DO PrintF, lun, strings[j]
Free_Lun, lun
Print, 'Time to rewrite file: ', systime(1) - start

END
;---------------------------------------------------

Cheers,

David

--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Re: confidence level
Next Topic: Write to a certain line of a file

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Fri Oct 10 10:21:38 PDT 2025

Total time taken to generate the page: 0.72720 seconds