Parse using VB.NET

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. Registered User
Join Date
Apr 2005
Posts
47

## 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 aline = Split(TextLine, " ")
TextBox2.Text = TextLine
If (aline = "cd") Then
End If

Loop

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

2. 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 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()

End If

3. 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...

4. Registered User
Join Date
Apr 2005
Posts
47

## it's not working

some how the program you send is not working as well

5. 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.

6. Registered User
Join Date
Apr 2005
Posts
47
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. I didn't run the code, but I believe the problem is this line:

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.

8. Registered User
Join Date
Apr 2005
Posts
47
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. 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...

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise