Results 1 to 7 of 7

Thread: Tab Delimiter?

  1. #1

    Thread Starter
    Addicted Member The Phoenix's Avatar
    Join Date
    Aug 2003
    Location
    With my wife
    Posts
    142

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

  2. #2
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    Is this what you wanted????

    VB Code:
    1. Imports System.IO
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Dim fsInput As FileStream
    5.         Dim srInput As StreamReader
    6.         Dim strLine As String
    7.         Dim strTmpArray() As String
    8.         'Array Postitions
    9.         Dim I As Short
    10.  
    11.         fsInput = New FileStream("c:\test.txt", FileMode.Open, FileAccess.Read)
    12.         srInput = New StreamReader(fsInput)
    13.  
    14.         strLine = srInput.ReadLine
    15.  
    16.         While Not (IsNothing(strLine))
    17.             strTmpArray = Split(strLine, Chr(9))
    18.  
    19.             For I = 0 To UBound(strTmpArray)
    20.                 MsgBox(strTmpArray(I))
    21.             Next
    22.             strLine = srInput.ReadLine
    23.         End While
    24.  
    25.         srInput.Close()
    26.         fsInput.Close()
    27.  
    28.     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

  3. #3

    Thread Starter
    Addicted Member The Phoenix's Avatar
    Join Date
    Aug 2003
    Location
    With my wife
    Posts
    142
    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:
    1. '''''
    2.             'Create the streamreader, delimiter, lines, and fields
    3.             Dim psrdDisplay As System.IO.StreamReader
    4.             '''''
    5.             'Here's the delimiter I need to change
    6.             Dim pchrDelimiter() As Char = {ToChar(",")}
    7.             Dim pstrline As String
    8.             Dim pstrFields() As String
    9.             Dim pintcount As Integer
    10.             '''''
    11.             'Use a preset fielname and read the line
    12.             psrdDisplay = New System.IO.StreamReader("C:\File.txt")
    13.             pstrline = psrdDisplay.ReadLine()
    14.             '''''
    15.             'Stope reading when we hit a blank line
    16.             Do Until pstrline = Nothing
    17.                 '''''
    18.                 'Otherwise spilt the line into fields
    19.                 ReDim Preserve EventRec(pintcount)
    20.                 pstrFields = pstrline.Split(pchrDelimiter)
    21.                 EventRec(pintcount).EventName = (pstrFields(0).ToString)
    22.                 cboEvents.Items.Add(EventRec(pintcount).EventName)
    23.                 EventRec(pintcount).EventDate = ToDateTime(pstrFields(1))
    24.                 EventRec(pintcount).EventPrice = ToDouble(pstrFields(2))
    25.                 EventRec(pintcount).EventDesc = (pstrFields(3).ToString)
    26.                 '''''
    27.                 'Increase the counter and read the next line
    28.                 pintcount += 1
    29.                 pstrline = psrdDisplay.ReadLine()
    30.             Loop
    31.             '''''
    32.             'Display a message bos when file is done opening and close
    33.             'the streamreader
    34.             MsgBox("Done Opening File", MsgBoxStyle.OKOnly, "Event Tracker")
    35.             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...

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    2.         Dim pintcount As Integer
    3.         'open file stream
    4.         Dim sr As New System.IO.StreamReader("C:\File.txt")
    5.         'get all lines/records
    6.         Dim lines() As String = sr.ReadToEnd.Split(ControlChars.NewLine)
    7.         'close the stream
    8.         sr.Close()
    9.         'read each line into class array
    10.         Dim line As String
    11.         For Each line In lines
    12.             'split line into fields
    13.             Dim fields() As String = line.Split(ControlChars.Tab)
    14.             'here is for commas
    15.             'Dim fields() As String = line.Split(",")
    16.             ReDim Preserve EventRec(pintcount)
    17.             EventRec(pintcount).EventName = (fields(0).ToString)
    18.             cboEvents.Items.Add(EventRec(pintcount).EventName)
    19.             EventRec(pintcount).EventDate = ToDateTime(fields(1))
    20.             EventRec(pintcount).EventPrice = ToDouble(fields(2))
    21.             EventRec(pintcount).EventDesc = (fields(3).ToString)
    22.             pintcount += 1
    23.         Next
    24.  
    25.         'Display a message box when file is done
    26.         MsgBox("Done Opening File", MsgBoxStyle.OKOnly, "Event Tracker")
    27.     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.

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    This is what I mean by the ArrayList bit (I just bluffed a dummt event record class):
    VB Code:
    1. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    2.         'open file stream
    3.         Dim sr As New System.IO.StreamReader("C:\File.txt")
    4.         'get all lines/records
    5.         Dim lines() As String = sr.ReadToEnd.Split(ControlChars.NewLine)
    6.         'close the stream
    7.         sr.Close()
    8.         'read each line into class array
    9.         Dim line As String
    10.         Dim al As New ArrayList
    11.         For Each line In lines
    12.             'split line into fields
    13.             Dim fields() As String = line.Split(ControlChars.Tab)
    14.             'here is for commas
    15.             'Dim fields() As String = line.Split(",")
    16.             Dim er As New EventRecord
    17.             er.EventName = (fields(0).ToString)
    18.             cboEvents.Items.Add(er.EventName)
    19.             er.EventDate = Convert.ToDateTime(fields(1))
    20.             er.EventPrice = Convert.ToDouble(fields(2))
    21.             er.EventDesc = (fields(3).ToString)
    22.             al.Add(er)
    23.         Next
    24.         Dim EventRec() As EventRecord = al.ToArray(GetType(EventRecord))
    25.  
    26.         'Display a message box when file is done
    27.         MsgBox("Done Opening File", MsgBoxStyle.OKOnly, "Event Tracker")
    28.     End Sub
    29.  
    30. End Class
    31.  
    32. Public Class EventRecord
    33.     Public EventName As String
    34.     Public EventDate As DateTime
    35.     Public EventPrice As Double
    36.     Public EventDesc As String
    37. End Class

  6. #6

    Thread Starter
    Addicted Member The Phoenix's Avatar
    Join Date
    Aug 2003
    Location
    With my wife
    Posts
    142
    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...

  7. #7
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    just change this line

    VB Code:
    1. 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
  •  



Click Here to Expand Forum to Full Width