File contents:
20.2 22.93
22.23 26.1
19.92 23.55
19.33 24.7
20.68 24.25
24.83 29.47
23.88 29.53
24.55 29.1
22.48 27.17
22.32 27.28
25.62 30.08
23.3 28.75
20.98 24.55
22.68 28.08
23.92 26.52
19.73 24.37
24.88 29.03
27.12 32.48
20.92 26.25
27.55 33.52
27.58 35.18
27.4 31.47
25.87 30.52
25 29.5
example code:
IDL> print, file_lines('sample1.txt')
24
IDL> openr, lun, 'sample1.txt', /get_lun
IDL> readf, lun, data1
IDL> print, data1
20.2000
IDL> readf, lun, data2
IDL> print, data2
22.2300
IDL> data = fltarr(2,24)
IDL> readf,lun,data
% READF: End of file encountered. Unit: 100, File: sample1.txt
% Execution halted at: $MAIN$
What happened? Read beyond the end of the file(because we had already read in the first 2 lines)
IDL> point_lun, lun, 0
IDL> readf,lun,data
IDL> print, min(data), max(data)
;PRO file
pro readfile
file='sample1.txt'
openr,lun,file, /get_lun
lines=file_lines(file)
thislat=fltarr(lines)
thislon=fltarr(lines)
temp1=0.0
temp2=0.0
point_lun, lun, 0
;header=''
;readf, lun, header
FOR j=0, lines-1 DO BEGIN
readf,lun, temp1, temp2
thislat[j]=temp1
thislon[j]=temp2
ENDFOR
close,lun
free_lun, lun
print, thislat, thislon
print, 'done!'
end
我碰到的這個問題原因是我沒有free_lun,lun