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

Home » Public Forums » archive » Random Access Text (like getline)
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Random Access Text (like getline) [message #39822] Tue, 22 June 2004 15:03 Go to next message
lefsky is currently offline  lefsky
Messages: 8
Registered: April 2002
Junior Member
I am looking for way to randomly access a text file, as getline will
do in python. Essentially, getline will allow you to access any line
in a text file by giving it the line number in the file (e.g.
getline(100)). I can't see a way to do that with a text file in
IDL....
Antw: Random Access Text (like getline) [message #39947 is a reply to message #39822] Thu, 24 June 2004 00:23 Go to previous message
Harald Jeszenszky is currently offline  Harald Jeszenszky
Messages: 9
Registered: January 1997
Junior Member
Hi Michael,

The following code reads consecutive blocks of text lines until the choosen
text line number is contained in the block. To change the performance you
can fiddle around with the buffer size (variable bufSize) but the given
value of 100 is a good starting point.

Regards,
Harald

;----------------------------------------------------------- ------------

FUNCTION getline, file, lineNum, ERROR=error

CATCH, error
IF (error NE 0) THEN BEGIN
MESSAGE, !ERROR_STATE.MSG, /CONTINUE
IF (N_ELEMENTS(unit) NE 0) THEN $
FREE_LUN, unit
RETURN, ''
ENDIF

; check parameters

IF (N_PARAMS() NE 2) THEN $
MESSAGE, 'Missing parameter.', /NONAME

lineNum = LONG(lineNum)
IF (lineNum LT 1) THEN $
MESSAGE, 'Line number starts from 1.', /NONAME

; read blocks of text lines

bufSize = 100L
lineBuf = STRARR(bufSize)
lineCnt = 0L

OPENR, unit, file, /GET_LUN
ON_IOERROR, END_OF_LOOP
WHILE (NOT EOF(unit)) AND (lineCnt LT lineNum) DO BEGIN
READF, unit, lineBuf
END_OF_LOOP:
lineCnt = lineCnt + (FSTAT(unit)).TRANSFER_COUNT
ENDWHILE
ON_IOERROR, NULL
FREE_LUN, unit

; return text line

IF (lineCnt LT lineNum) THEN $
MESSAGE, 'End of file reached.', /NONAME

RETURN, lineBuf[(lineNum - 1) MOD bufSize]

END ; getline

;----------------------------------------------------------- ------------

>>> Michael Lefsky<lefsky@cnr.colostate.edu> 23.06.04 00:03:29 >>>
I am looking for way to randomly access a text file, as getline will
do in python. Essentially, getline will allow you to access any line
in a text file by giving it the line number in the file (e.g.
getline(100)). I can't see a way to do that with a text file in
IDL....
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: iimage tool
Next Topic: Re: Generate 3D Surface out of Points in Space

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

Current Time: Wed Oct 08 13:36:28 PDT 2025

Total time taken to generate the page: 0.00607 seconds