none
Read file to array? RRS feed

  • Question

  • I am new to SB and would like to read a text file so it can be manipulated by a program. I have not seen the usual [loop until EOL] constrict. So, I think I want to read the file into an array.

    Perhaps there is a better way I do not know about. If not, then how do I read a file into an array to be manipulated, and how would I write the array to a file?

    Thanks - jwzumwalt




    • Edited by jwzumwalt1 Monday, August 21, 2017 8:17 PM
    Monday, August 21, 2017 8:13 PM

Answers

  • Here are 2 variants: 1.) without  and 2.)  with LitDev extension

    PS: Added variants to write array(s) to file(s) then and read back again

    path = Program.Directory +"\MyFile.txt" ' 1.) no Extension

    n = 1 line = File.ReadLine(path, n) While line <> "" arr1[n] = line

    n = n + 1

    line = File.ReadLine(path, n) EndWhile ' 2.) with LitDev extension arr2 = LDFile.ReadToArray(path) ' Show values in both arrays arr1 & arr2 For i = 1 To Array.GetItemCount(arr1) TextWindow.WriteLine(arr1[i]) ' 1.) TextWindow.ForegroundColor = 11 TextWindow.WriteLine(arr2[i]) ' 2.) TextWindow.ForegroundColor = 10 EndFor ' ======================================================== ' WRITE array arr1 to NewFile1.txt (line by line) newPath1 = Program.Directory +"\NewFile1.txt" For i = 1 To n File.WriteLine(newPath1, i, arr1[i]) EndFor ' write whole array arr2 to NewFile2.txt newPath2 = Program.Directory +"\NewFile2.txt" File.WriteContents(newPath2, arr2) ' read back whole array from NewFile2.txt to arr3 arr3 = File.ReadContents(newPath2) TextWindow.WriteLine("") TextWindow.ForegroundColor = 14 For i = 1 To Array.GetItemCount(arr3) TextWindow.WriteLine(arr3[i]) EndFor TextWindow.ForegroundColor = 10 'TextWindow.Read()



    Monday, August 21, 2017 8:54 PM
    Answerer

All replies

  • Here are 2 variants: 1.) without  and 2.)  with LitDev extension

    PS: Added variants to write array(s) to file(s) then and read back again

    path = Program.Directory +"\MyFile.txt" ' 1.) no Extension

    n = 1 line = File.ReadLine(path, n) While line <> "" arr1[n] = line

    n = n + 1

    line = File.ReadLine(path, n) EndWhile ' 2.) with LitDev extension arr2 = LDFile.ReadToArray(path) ' Show values in both arrays arr1 & arr2 For i = 1 To Array.GetItemCount(arr1) TextWindow.WriteLine(arr1[i]) ' 1.) TextWindow.ForegroundColor = 11 TextWindow.WriteLine(arr2[i]) ' 2.) TextWindow.ForegroundColor = 10 EndFor ' ======================================================== ' WRITE array arr1 to NewFile1.txt (line by line) newPath1 = Program.Directory +"\NewFile1.txt" For i = 1 To n File.WriteLine(newPath1, i, arr1[i]) EndFor ' write whole array arr2 to NewFile2.txt newPath2 = Program.Directory +"\NewFile2.txt" File.WriteContents(newPath2, arr2) ' read back whole array from NewFile2.txt to arr3 arr3 = File.ReadContents(newPath2) TextWindow.WriteLine("") TextWindow.ForegroundColor = 14 For i = 1 To Array.GetItemCount(arr3) TextWindow.WriteLine(arr3[i]) EndFor TextWindow.ForegroundColor = 10 'TextWindow.Read()



    Monday, August 21, 2017 8:54 PM
    Answerer
  • Thanks for the help...

    [ ignore - I wrote this test file and for some reason it prints the first line of the file twice. What causes this?]

    NOTE: This listing has been edited and had errors corrected. It now performs correctly and may be used as a temple. (copied and pasted)

    ' load file to array example

    CRLF = Text.GetCharacter(10) + Text.GetCharacter(13) fileName = "test.dat" ' create test file TextWindow.WriteLine(CRLF + "Creating text file...") ' init new empty file File.WriteContents(fileName, "") ' create empty file for i = 1 to 10 TextWindow.WriteLine("Write to file: " + File.AppendContents(fileName, "Line " + i +": Hello, Cruel world!")) EndFor ' read test file n = 1 line = File.ReadLine(fileName, n) While line <> "" ' will stop at first empty line fileArray[n] = line n = n + 1 line = File.ReadLine(fileName, n) EndWhile ' show file array contents TextWindow.WriteLine(CRLF + "Show array contents of file...") fileLength = Array.GetItemCount(fileArray) TextWindow.WriteLine("lines in file = " + fileLength + CRLF) For j = 1 to fileLength TextWindow.WriteLine(fileArray[j]) EndFor

    • Edited by jwzumwalt1 Tuesday, August 22, 2017 10:06 AM
    Tuesday, August 22, 2017 2:01 AM
  • Ok, i see, the 1st line is read twice to the array. Please use

    ...

    ...

    ' read test file
    n = 1
    line = File.ReadLine(txtFile, n)
    While line <> ""    ' will stop at first empty line
      fileArray[n] = line
      n = n + 1
      line = File.ReadLine(txtFile, n)
    EndWhile

    'TextWindow.WriteLine("n = "+ n)
    'TextWindow.WriteLine("cnt = "+ Array.GetItemCount(fileArray))
    'TextWindow.WriteLine("Content:"+ CRLF + fileArray)

    ...

    ...

    * I've also changed it in the sample above *

    Tuesday, August 22, 2017 9:37 AM
    Answerer
  • Thanks! It works now :)

    I also corrected my listing so someone else can use it as a template.

    Tuesday, August 22, 2017 10:03 AM