The traditional approach in IDL is to read all the lines of the file into one string array with one single, simple read command, as follows:
nLines = file_lines('my_ascii_text_file.txt') ; Query the number of lines in the file
textArray = strarr(nLines) ; Allocate your storage for the string array
openr, lun, 'my_ascii_text_file.txt', /GET_LUN
readf, lun, textArray ; Reads the entire contents into the string array
free_lun, lun
line5text = textArray[4] ; e.g., get the 5th line of text in the file
If you really want to read a text file in a loop one line at a time, then the below would be an alternative approach:
openr, lun, 'my_ascii_text_file.txt', /GET_LUN
currentLineText = ''
for i = 0, someNumberOfLines-1 do begin
; Read just one line from the file and move the file pointer to the next line
readf, lun, currentLineText
print, 'Line ', strtrim(i, 2), ' text = ', currentLineText
endfor
free_lun, lun
|