Parse using VB.NET


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Parse using VB.NET

  1. #1
    Join Date
    Apr 2005
    Posts
    47

    Smile Parse using VB.NET

    If we want to just parse some data from a file and write those specific datas into txt file what is the solution

    like if i have this file:
    ---------------------------------
    Info 1
    -+-+-+-+-+-+
    pa cd lm
    3 | 3 | 4
    2 | 6 | 9


    and if i just want cd and the numbers below it "3, 6"
    how can i process it
    I have this code:
    Dim FILE_NAME As String = "C:\text.txt"

    Dim textdelimiter As String

    Dim file_line As String

    textdelimiter = " "
    If System.IO.File.Exists(FILE_NAME) = True Then
    Dim objReader As New System.IO.StreamReader(FILE_NAME)
    TextBox1.Text = objReader.ReadToEnd
    Do While objReader.Peek() > -1
    TextLine = TextLine & objReader.ReadLine() & vbNewLine
    Dim aline = Split(TextLine, " ")
    TextBox2.Text = TextLine
    If (aline = "cd") Then
    Dim oReader As StreamWriter
    oReader = New StreamWriter("C:\p.txt")
    oReader.Close()
    End If

    Loop



    this code is not working and i need help over it
    appriciat any help

  2. #2
    Join Date
    Jan 2004
    Location
    Alexandria, VA
    Posts
    392
    You need to loop through the members of aline, ie:

    Code:
            Dim FILE_NAME As String = "C:\text.txt"
    
            Dim textdelimiter As String
    
            Dim file_line As String
            Dim intCol As Int16
            Dim X As Integer
            Dim oWriter As System.IO.StreamWriter
    
            textdelimiter = " "
            If System.IO.File.Exists(FILE_NAME) = True Then
                Dim objReader As New System.IO.StreamReader(FILE_NAME)
                TextBox1.Text = objReader.ReadToEnd
                Do While objReader.Peek() > -1
                    TextLine = TextLine & objReader.ReadLine() & vbNewLine
                    Dim aline() = Split(TextLine, " ")
                    TextBox2.Text = TextLine
                    ' Loop through column values
                    For X = 0 To aline.Length - 1
                        ' If this is the CD column, then set the column indicator
                        ' for the lines that follow.
                        If (aline(X) = "cd") Then
                            ' Open the destination
                            oWriter = New System.IO.StreamWriter("C:\p.txt")
                            ' get the CD column number
                            intCol = X
                            Exit For
                        End If
                    Next
                    ' If we've set the CD column already, we must be into the 
                    ' data section...
                    If intCol <> 0 Then
                        ' Write just the CD column data value
                        oWriter.Write(aline(intCol) & vbCrLf)
                    End If
                Loop
                ' Close the writer
                oWriter.Close()
                ' Close the reader
                objReader.Close()
    
            End If
    Bob Rouse
    Dimension Data

  3. #3
    Join Date
    Jan 2004
    Location
    Alexandria, VA
    Posts
    392
    UPDATE: I just realized that if the first column is the one you want, it won't work because the "intCol" value will be set to "0". So to make it foolproof, set intCol = "-1" at the beginning...
    Bob Rouse
    Dimension Data

  4. #4
    Join Date
    Apr 2005
    Posts
    47

    Exclamation it's not working

    some how the program you send is not working as well

  5. #5
    Join Date
    Jan 2004
    Location
    Alexandria, VA
    Posts
    392
    Can you be a little more specific? And possibly I didn't understand what you wanted as a result. Perhaps you can post an example of the result file that you want.
    Bob Rouse
    Dimension Data

  6. #6
    Join Date
    Apr 2005
    Posts
    47

    Lightbulb

    yes ofcourse,

    from what i have:
    ---------------------------------
    Info 1
    -+-+-+-+-+-+
    pa cd lm
    3 | 3 | 4
    2 | 6 | 9
    I want to get:
    cd
    3
    6

    that is all

  7. #7
    Join Date
    Jan 2004
    Location
    Alexandria, VA
    Posts
    392
    I didn't run the code, but I believe the problem is this line:

    TextBox1.Text = objReader.ReadToEnd

    This will move the file position pointer to the end of the file, the very first "objReader.Peek()" will return a -1. Try commenting that line out.
    Bob Rouse
    Dimension Data

  8. #8
    Join Date
    Apr 2005
    Posts
    47

    Cool

    thank you,
    also it gives me this message. do u know why?

    "Additional information: The process cannot access the file "C:\p.txt" because it is being used by another process."



    thank you,

  9. #9
    Join Date
    Jan 2004
    Location
    Alexandria, VA
    Posts
    392
    With VS.NET closed, can you delete the file? It might still be held open by a process that got left when the program crashed. This would require a reboot.

    If you can delete it, then step through the program and see what is going on. It would seem that the line "oWriter = New System.IO.StreamWriter("C:\p.txt")" is being called twice, but I can't imagine why...
    Bob Rouse
    Dimension Data

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center