| Re: Random Access Text (like getline) [message #39821 is a reply to message #39815] |
Tue, 22 June 2004 15:48  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Michael Lefsky writes:
> 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....
Here is something quick and dirty, hacked from something else.
Seems to work with the light testing I've given it on a couple
of text files. Might get you started in the right direction,
anyway. :-)
Cheers,
David
;*********************************************************** *
FUNCTION GetLine, lineNumber, filename
; This utility routine is used to return a specific line in a text file.
IF N_Elements(maxrows) EQ 0 THEN maxrows = 500L
IF N_Elements(lineNumber) EQ 0 THEN lineNumber = 0
IF N_Elements(filename) EQ 0 THEN BEGIN
filename = Dialog_Pickfile(Title='Find Line in this File...')
IF filename EQ "" THEN RETURN, ""
ENDIF
OpenR, lun, filename, /Get_Lun
Catch, error
IF error NE 0 THEN BEGIN
count = count-1
Free_Lun, lun
ok = Dialog_Message('Selected text file has only ' + $
StrTrim(count,2) + ' lines in the file.')
RETURN, ""
ENDIF
RESTART:
count = 0L
line = ''
REPEAT BEGIN
ReadF, lun, line
count = count + 1
; Try again if you hit MAXROWS without encountering the
; end of the file. Double the MAXROWS parameter.
IF count EQ maxrows THEN BEGIN
maxrows = maxrows * 2
Point_Lun, lun, 0
GOTO, RESTART
ENDIF
ENDREP UNTIL count GT lineNumber
Free_Lun, lun
RETURN, line
END
;*********************************************************** *
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
|
|
|
|