|
-
Aug 10th, 2003, 10:54 PM
#1
Thread Starter
Addicted Member
Tab Delimiter?
Greetings everyonel!
I lurk around here from time to time, but now I have a question.
I need to work with a tab-delimited file, but I dont know how to declare the Delimiter as a tab. I know how to work with comma delimiters and such, so I just need to know how to declare a tab as delimiter. I know its probably painfully obvious, but any help would be appreciated.
Thanks!
Take my love
Take my land
Take me where I cannot stand
I don't care, I'm still free
You can't take the sky from me...
-
Aug 11th, 2003, 10:06 AM
#2
Is this what you wanted????
VB Code:
Imports System.IO
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim fsInput As FileStream
Dim srInput As StreamReader
Dim strLine As String
Dim strTmpArray() As String
'Array Postitions
Dim I As Short
fsInput = New FileStream("c:\test.txt", FileMode.Open, FileAccess.Read)
srInput = New StreamReader(fsInput)
strLine = srInput.ReadLine
While Not (IsNothing(strLine))
strTmpArray = Split(strLine, Chr(9))
For I = 0 To UBound(strTmpArray)
MsgBox(strTmpArray(I))
Next
strLine = srInput.ReadLine
End While
srInput.Close()
fsInput.Close()
End Sub
sample file c:\test.txt
Code:
Name Phone#
friend1 123-4567
friend2 123-4568
friend3 123-4569
it will display Name, Phone#, friend1 ....
EDIT: for got the Inports statement!!!!
Last edited by <ABX; Aug 12th, 2003 at 12:07 AM.
Tips:
- Google is your friend! Search before posting!
- Name your thread appropriately... "I Need Help" doesn't cut it!
- Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
- Allways Include the Name and Line of the Exception (if one is occuring!)
- If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)
If you think I was helpful, rate my post  IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous
-
Aug 11th, 2003, 05:30 PM
#3
Thread Starter
Addicted Member
Well, I'm gonna do exactly what your signature says: Open my mouth and prove myself an idiot.
You must be using a different Version of .NET than me, cause that code is very unfamiliar. I use .NET Framework 1 version 1.03705.
Here's my code for a comma-delimited file I'm reading:
VB Code:
'''''
'Create the streamreader, delimiter, lines, and fields
Dim psrdDisplay As System.IO.StreamReader
'''''
'Here's the delimiter I need to change
Dim pchrDelimiter() As Char = {ToChar(",")}
Dim pstrline As String
Dim pstrFields() As String
Dim pintcount As Integer
'''''
'Use a preset fielname and read the line
psrdDisplay = New System.IO.StreamReader("C:\File.txt")
pstrline = psrdDisplay.ReadLine()
'''''
'Stope reading when we hit a blank line
Do Until pstrline = Nothing
'''''
'Otherwise spilt the line into fields
ReDim Preserve EventRec(pintcount)
pstrFields = pstrline.Split(pchrDelimiter)
EventRec(pintcount).EventName = (pstrFields(0).ToString)
cboEvents.Items.Add(EventRec(pintcount).EventName)
EventRec(pintcount).EventDate = ToDateTime(pstrFields(1))
EventRec(pintcount).EventPrice = ToDouble(pstrFields(2))
EventRec(pintcount).EventDesc = (pstrFields(3).ToString)
'''''
'Increase the counter and read the next line
pintcount += 1
pstrline = psrdDisplay.ReadLine()
Loop
'''''
'Display a message bos when file is done opening and close
'the streamreader
MsgBox("Done Opening File", MsgBoxStyle.OKOnly, "Event Tracker")
psrdDisplay.Close()
Now, all I need to know is how to use a TAB instead of a comma for the delimiter. Surely there's an easy way of doing that right?\
Hope that clears it up.
Thanks!
Last edited by The Phoenix; Aug 11th, 2003 at 05:35 PM.
Take my love
Take my land
Take me where I cannot stand
I don't care, I'm still free
You can't take the sky from me...
-
Aug 11th, 2003, 05:53 PM
#4
There is just more than one way to skin a cat thats all. His code should work with your framework. Here is another way:
VB Code:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim pintcount As Integer
'open file stream
Dim sr As New System.IO.StreamReader("C:\File.txt")
'get all lines/records
Dim lines() As String = sr.ReadToEnd.Split(ControlChars.NewLine)
'close the stream
sr.Close()
'read each line into class array
Dim line As String
For Each line In lines
'split line into fields
Dim fields() As String = line.Split(ControlChars.Tab)
'here is for commas
'Dim fields() As String = line.Split(",")
ReDim Preserve EventRec(pintcount)
EventRec(pintcount).EventName = (fields(0).ToString)
cboEvents.Items.Add(EventRec(pintcount).EventName)
EventRec(pintcount).EventDate = ToDateTime(fields(1))
EventRec(pintcount).EventPrice = ToDouble(fields(2))
EventRec(pintcount).EventDesc = (fields(3).ToString)
pintcount += 1
Next
'Display a message box when file is done
MsgBox("Done Opening File", MsgBoxStyle.OKOnly, "Event Tracker")
End Sub
Except if I knew what type the items of the EventRec array were I'd just use an ArrayList then cast them to an array of the specific type.
-
Aug 11th, 2003, 06:04 PM
#5
This is what I mean by the ArrayList bit (I just bluffed a dummt event record class):
VB Code:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'open file stream
Dim sr As New System.IO.StreamReader("C:\File.txt")
'get all lines/records
Dim lines() As String = sr.ReadToEnd.Split(ControlChars.NewLine)
'close the stream
sr.Close()
'read each line into class array
Dim line As String
Dim al As New ArrayList
For Each line In lines
'split line into fields
Dim fields() As String = line.Split(ControlChars.Tab)
'here is for commas
'Dim fields() As String = line.Split(",")
Dim er As New EventRecord
er.EventName = (fields(0).ToString)
cboEvents.Items.Add(er.EventName)
er.EventDate = Convert.ToDateTime(fields(1))
er.EventPrice = Convert.ToDouble(fields(2))
er.EventDesc = (fields(3).ToString)
al.Add(er)
Next
Dim EventRec() As EventRecord = al.ToArray(GetType(EventRecord))
'Display a message box when file is done
MsgBox("Done Opening File", MsgBoxStyle.OKOnly, "Event Tracker")
End Sub
End Class
Public Class EventRecord
Public EventName As String
Public EventDate As DateTime
Public EventPrice As Double
Public EventDesc As String
End Class
-
Aug 11th, 2003, 07:07 PM
#6
Thread Starter
Addicted Member
Thank you very much, both of you! I'll have to mess around with that a little to figure it all out, but I think I now know how to do it.
Thanks!
Take my love
Take my land
Take me where I cannot stand
I don't care, I'm still free
You can't take the sky from me...
-
Aug 12th, 2003, 12:05 AM
#7
just change this line
VB Code:
Dim pchrDelimiter() As Char = {ToChar(chr(9))}
not sure if thats proper but that should work
that was my first attempt at reading a file in vb .net so i dont expect it to be pecfect but it worked - amazingly
Tips:
- Google is your friend! Search before posting!
- Name your thread appropriately... "I Need Help" doesn't cut it!
- Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
- Allways Include the Name and Line of the Exception (if one is occuring!)
- If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)
If you think I was helpful, rate my post  IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|