Listing a file with gaps

jschuchert

Active Member
Licensed User
Longtime User
Hi guys. Sorry I couldn't think of a more definitive title. I have a text file to which I have been appending points with their data. Now I want to list the file's information. However, there are gaps between points so the code below does not work properly unless there are no gaps. I am getting an 'Array out of bounds' error. Any ideas? I am adding the points to the file with this code:
B4X:
Dim writer As TextWriter 
writer.Initialize (File.openoutput(File.DirInternal,main.strfilename ,True))
writer.Write(main.strpointno & "," & main.dblnorth & "," & main.dbleast & "," & main.strdes & CRLF)
writer.Close

The kind of file I am talking about would look like this
1,1000,1000,NONE
2,1027.699,1030.228,NONE
3,1030.228,1070.797,NONE
4,999.933,1081.15,NONE
11,1088.234,7654.987,NONE 'gaps appear between 4 and 11, 12 and 33
12,1345.234,3456.789,NONE
33,1023.456,8765.234,NONE

B4X:
Sub btnOK_click
Dim crd()
'This is so all of the points in the file will list when the text box is empty

Dim reader As TextReader 
reader.Initialize (File.openinput(File.DirInternal,main.strfilename ))
Dim lineoftext As String
Dim cline()
lineoftext=reader.ReadLine 
Do While lineoftext<>Null
cline=Regex.Split (",",lineoftext)
txtdisplay.text=txtdisplay.text & cline(0) & "  " & NumberFormat(cline(1),0,3) & "  " & NumberFormat(cline(2),0,3) & "  " & cline(3) & CRLF
lineoftext=reader.readline
Loop
reader.Close
End If
 
'when there is a range of points separated with a comma
 Dim firstpt,lastpt,crd()
 Dim reader As TextReader 
reader.Initialize (File.openinput(File.DirInternal,main.strfilename ))
Dim lineoftext As String
Dim cline()
crd=Regex.Split(",",txtrange.Text )
firstpt=crd(0)
lastpt=crd(1)
lineoftext=reader.ReadLine 
Do While lineoftext<>Null
cline=Regex.Split(",",lineoftext )
If cline(0)>=firstpt AND cline(0)<=lastpt Then
txtdisplay.Text = txtdisplay.Text & cline(0) & " " & NumberFormat(cline(1),0,3) & " " & NumberFormat(cline(2),0,3) & " " & cline(3) & CRLF
End If
lineoftext=reader.readline
Loop
reader.Close
End Sub
 

jschuchert

Active Member
Licensed User
Longtime User
Thanks, Erel, for your response but not sure it will satisfy other routines in my app that use the file. Would I still be able to use the text reader to parse each line? Currently I can list all of the points including the gaps but can't parse the lines. In b4ppc, I could bypass a blank line by using 'goto' to a label just before 'loop'. I will experiment with the csv file approach anyway. I know you are busy but perhaps a snippet of code based upon my scenario would be helpful.

Jim Schuchert
 
Upvote 0

jschuchert

Active Member
Licensed User
Longtime User
I believe I have satisfied my issue with a workaround using the text reader and writer approach. The critical data is still displayed, but not parsed. IMO that really wasn't too important. I am also able to display a range of points regardless of the gaps between them. Thanks to all who looked or spent any time in my behalf.

Jim Schuchert
 
Upvote 0
Top