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 #78957 is a reply to message #78956] Thu, 19 January 2012 09:49 Go to previous messageGo to previous message
Michael Galloy is currently offline  Michael Galloy
Messages: 1114
Registered: April 2006
Senior Member
On 1/19/12 9:43 AM, Mark Piper wrote:
> On 1/19/2012 5:18 AM, Brian J. Daniel wrote:
>> I haven't done this, but POINT_LUN may do what you want. You'd need
>> to know the number of bytes per line. You will still have to read the
>> entire line, change the bit you want to change, move the LUN back to
>> the beginning of the line, and rewrite it.
>>
>> OpenR, LUN, filename, /Get_Lun
>> Point_Lun, LUN, line_number * bytes_per_line
>> tmp=''
>> ReadF, LUN, tmp
>> ; change the line as you see fit
>> new_line = tmp + 'changed line!'
>> Point_Lun, LUN, line_number * bytes_per_line
>> PrintF, LUN, new_line
>> Free_Lun, LUN
>>
>> Code is UNTESTED!
>
> To add to Brian's idea, if you have a text file, use SKIP_LUN with the
> LINES keyword set. A crude example:
>
> pro replace_line_ex
> compile_opt idl2
>
> f0 = 'ascii.txt'
> f1 = 'ascii-1.txt'
> file_copy, file_which(f0), f1, /overwrite
>
> nskip = 7
> line = ''
> replacement = 'My dog has fleas.'
>
> openu, u, f1, /get_lun
> skip_lun, u, nskip, /lines
> point_lun, -u, mark
> readf, u, line
> nchars = strlen(line)
> point_lun, u, mark
> printf, u, replacement, format='(a-' + strtrim(nchars,2) + ')'
> free_lun, u
> end
>
> Diff 'ascii.txt' in the example/data directory of the IDL distro with
> 'ascii-1.txt' in your working directory.

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?


Mike
--
Michael Galloy
www.michaelgalloy.com
Modern IDL, A Guide to Learning IDL: http://modernidl.idldev.com
Research Mathematician
Tech-X Corporation
[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: Sat Oct 11 08:01:55 PDT 2025

Total time taken to generate the page: 1.43846 seconds