Notifications
Clear all

[Closed] Problems with filestream

Hi I am trying read and write information to/from a text file

the text file is “c:\array1.txt” and contains

MainArray = #(“one”,“two”,“three”,“four”)
array1 = #(“a”,“b”,“c”)
array2 = #(“d”,“e”,“f”)
array3 = #(“g”,“h”,“i”)
array4 = #(“j”,“k”,“l”)

eventually both scripts will be combined but for now the first script reads the info

#####################################################
ArrayFile = OpenFile “c:\array1.txt” mode:“r+”

   SkipToString ArrayFile "MainArray = #("
   MainBegin = FilePos ArrayFile 
   SkipToString ArrayFile ")"
   MainEnd = FilePos ArrayFile - 2
   MainList = MainEnd - MainBegin 
   Seek ArrayFile MainBegin
   MainListText = ReadChars ArrayFile MainList errorAtEOF:false
   MainArray = FilterString MainListText "\","    
   
   GroupArrays = #()    
   for i in 1 to MainArray.count do
       (    
           num = i as string
           skiptostring ArrayFile ("array"+num+" = #(")
           GroupBeg = filepos ArrayFile
           skiptostring ArrayFile ")"            
           GroupEnd = filepos ArrayFile - 2
           GroupList = GroupEnd - GroupBeg         
           seek ArrayFile GroupBeg
           GroupListText = readchars ArrayFile GroupList errorAtEOF:false        
           Groupname = filterstring GroupListText "\","
           Sort Groupname    
           GroupArrays[i] = Groupname    
           seek ArrayFile 0    
       )

###########################################################

This seems to work fine.

The second script is suppossed to clear the file but does not work correctly.

#######################################################

   for i in 1 to MainArray.count do
       (    
           num = i as string
           skiptostring ArrayFile ("array"+num+" = #(")
           GroupBeg = filepos ArrayFile
           skiptostring ArrayFile ")"            
           GroupEnd = filepos ArrayFile - 2
           GroupList = GroupEnd - GroupBeg         
           seek ArrayFile GroupBeg
           
           For ii in 1 to grouplist do
               (
                   Format " " to:ArrayFile
               )
       )

######################################################

The result is this

MainArray = #(“one”,“two”,“three”,“four”)
array1 = #( “)
array2 = #(“d”,“e”,“f”)
array3 = #( “)
array4 = #(“j”,“k”,“l”)

I cannot work out why only 2 arrays have been cleared. Does anyone have any ideas?

2 Replies

The way I usually work with filestreams is to get all the info in the file in a string, filter it or do whatever it needs to be done to it, and write it back to the file. I find it much easier to work with array elements than trying to continuously read and write to a file. Or another easy way to deal with textfiles is to work with getinisetting and setinisetting.

I think the ini settings will suit my purpose.

Thanks Anton