Search code examples
arraysfileautoitmessageboxfile-read

How to read from file to array


Trying to read from a txt file and have the results be displayed in the message box. I plan on copying and pasting lines of 1000 and deleting them from the array, later in my code. For now I'd like to be able to see that the file can be read into the array and be displayed:

Local $List
FileReadToArray( "C:/Users/Desktop/recent_list.txt", $List [, $iFlags = $FRTA_COUNT [, $sDelimiter = ""] ])
MsgBox( 0, "Listing", $List )

I get an error: >"C:\Program Files (x86)\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "C:\Users\Documents\Test.au3"


Solution

  • "FileReadToArray" has no other parameters than the file to read! You have used the function call from "_FileReadToArray". The square brackets in the function line means: This parameters are optional! If you want to use them with the default values, its not required to write them in the function call. And "FileReadToArray" reads the content of a file into an array. Thats why your call should look like so:

    Local $arList = FileReadToArray("C:/Users/Desktop/recent_list.txt")
    
    ; to show every line in a MsgBox you must iterate 
    ; through the result array
    For $i = 0 To UBound($arList) -1
        ; MsgBox is not sensefull with hundred of lines in file! 
        ; MsgBox(0, 'Line ' & $i+1, $arList[$i])
    
        ; better way - console output
        ConsoleWrite('['& $i+1 & '] ' & $arList[$i] & @CRLF)  
    Next