• Support
  • Articles
  • Resources
  • Products

Read file [lines] into a string array

Moderator: boris

1 postPage 1 of 1

stevo
Newbie

Posts:
19
Joined: Tue Oct 15, 2013 11:08 pm
by stevo » Tue Aug 12, 2014 1:46 am
Hope you find this useful and that it inspires some more interesting script solutions!

Code: Select all

  1. ; Opens a file and reads each line into an array
  2. ; Each line must first be added to a temporary variable which is counted so we know how big to make the array
  3. ; This does NOT
  4. closett
  5. :Main
  6.     Yes = 1 ; for YesNoBox result
  7.     No = 0 ; for YesNoBox result
  8.     BlankLine = 0
  9.     call OpenFile
  10.     call ReadFile
  11.     call CloseFile
  12.     call PopulateArray
  13. END
  14.  
  15. /* <================ SUB ROUTINES BELOW ================> */
  16. :OpenFile
  17.     FileNameBox 'Choose a file to open/import'
  18.     FileChosen = result
  19.     if FileChosen = Yes then
  20.         FileOpen fhandle inputstr 0
  21.     else
  22.         messagebox 'You clicked [Cancel]' 'No file selected'
  23.         end
  24.     endif
  25. RETURN
  26. ;-----------------------------------------------------------
  27. :ReadFile
  28.     YesNoBox 'Do you want to skip blank lines?' 'Skip blanks?'
  29.     SkipBlanks = result
  30.     counter = 0
  31.     while 1
  32.         ; Read a line from the file.
  33.         filereadln fhandle line
  34.         EndOfFile = result
  35. ;messagebox EndOfFile 'EndOfFile: 0=No; 1=Yes'
  36.         if EndOfFile = Yes then ; <--- this HAS to precede the line length check below ('StrLen line') else loop gets stuck in that when it reaches the EndOfFile
  37. ;messagebox 'Break out of while loop' 'EndOfFile = Yes'
  38.             break
  39.         endif
  40.         StrLen line
  41.         LineLen = result
  42. ;messagebox LineLen 'LineLen'
  43.         if SkipBlanks = Yes then
  44.             if LineLen = BlankLine then
  45. ;messagebox 'blank line found - skipping' 'LineLen = BlankLine'
  46.                 continue
  47.             elseif LineLen = 511 then
  48.                 messagebox 'The current line might be too long! Please check the length is less than 511 characters' 'WARNING: line too long'
  49.             endif
  50.         endif
  51.         sprintf2 tmpvar 'var%d = line' counter
  52.         execcmnd tmpvar
  53.         counter = counter + 1 ; since arrays are zero(0) based, increment counter after the 1st line
  54.         ; Repeat until the end of the file.
  55.     endwhile
  56. RETURN
  57. ;-----------------------------------------------------------
  58. :CloseFile
  59.     FileClose fhandle
  60. RETURN
  61. ;-----------------------------------------------------------
  62. :PopulateArray
  63.     arraysize = counter
  64.     StrDim lines arraysize
  65.     For x 0 counter - 1
  66.         sprintf2 addline 'lines[%d] = var%d' x x
  67.         execcmnd addline
  68.         sprintf2 output '%s\n%d: %s' output x lines[x]
  69.     Next
  70.     StrSpecial output
  71.     messagebox output '1st few array entries (511 characters total output)'
  72. RETURN


Feedback is appreciated :)

1 postPage 1 of 1

Users browsing this forum: No registered users