X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 26 Oct 2006 07:00 PM by  anon
read a file per line
 1 Replies
Sort:
You are not authorized to post a reply.
Author Messages

anon



New Member


Posts:
New Member


--
26 Oct 2006 07:00 PM
    i want to read a txt. file per line, how can i do this? thank you

    Deleted User



    New Member


    Posts:
    New Member


    --
    26 Oct 2006 07:00 PM
    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
    You are not authorized to post a reply.