text file parse

Ace Master

Well-known member
Joined
Aug 28, 2003
Messages
140
hi.

Who can give me a good example of text file parse ?

I have a text file and I whant to take some parts of it and put into some variables.

thanks a lot.
 
it is possible to create an array with text file elements and then to show in the form just the array element that I want?
 
What does the contents of the text file look like? Are we talking some kind of structured file (TSV, CSV etc) or a more free form layout?
 
this is how the file looks:

1 44.12345 25.56789 Ianca
2 41.78909 24.56789 Bechet

values are separated by "space"

for row 1 I have 44.12345 > var1 25.56789 > var2 Ianca > var3

etc....for the rest of the rows.

is a matter of life and death :)
 
Code:
      Dim sr As New System.IO.StreamReader("c:\test.txt")
        Dim line As String
        Dim vals() As String

        Do
            line = sr.ReadLine()
            If line Is Nothing Then Exit Do
            vals = line.Split(" ")     vals will contain an array of the data for each line.
        Loop While Not line Is Nothing

bit rough but should do the trick
 
thanks, but how I take the values ?

for vals(1) I receive 41.78909 for 0 is ok...for 2 or 3 is ok..but the problem is that the array contain the last line (second line)

I cant extract from array elements from first line.

a little help here.....thanks
 
Last edited by a moderator:
enter problem

I see that I have aother problem:

The vals(5) for ex. is the last from the line and this variable contain an "enter" , and I want to ignore this "enter" from the array.

One way is to take it out from the file source but it looks bad.

what I need to do ?:confused:
 
Last edited by a moderator:
I tried to make a bidimensional array to put some values like this :

Code:
       Dim sr As New System.IO.StreamReader("ace.txt")
       Dim vals() As Object
       Dim Line As Object

        Line = sr.ReadToEnd
        vals = Line.Split("," & vbCrLf)
        sr.Close()

text file is like this:

1,158
2,258
3,689
4,489
5,789

I whant this:

TextBox1.Text = vals(1)(2) to return 158
TextBox1.Text = vals(3)(2) to return 689
etc...

but..it seams not to work...
 
you might want to try the ReadLine() property in this way ....
Code:
Dim sr As New System.IO.StreamReader(New System.IO.FileStream("C:\ace.txt",System.IO.FileMode.Open))

While Not sr.Peek()
    Console.WriteLine(sr.ReadLine.Split(",")(1))
    /// you can use the Replace function here if you have returns to remove ( eg vbcrlf , chr(10) , chr(13) and so on )
    /// for example Console.WriteLine(Replace(sr.ReadLine.Split(",")(1) , VbCrlf , "")) 
End While

sr.Close()
 
Back
Top