Results 1 to 9 of 9

Thread: [RESOLVED] Reading comma delimited file

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    4

    Resolved [RESOLVED] Reading comma delimited file

    Hi,I'm new to visual basic programming, I'm trying to read a comma delimited file and assign the data to arrays for making a sales summary, can anyone give me some sugguestion about where to start? Thanks

    the data looks like the following, where the first number is the sales ID, the rest are sales amount and the -1.0 indicates the end of the line.
    I need to put the IDs in ID() and sum the total sales into TotalSales(), but I have problem reading in and summing up the data.
    Code:
    282, 8848.19, 8160.08, 2404.85, 6791.34, -1.0
    721, 776.82, 6542.79, 2659.21, -1.0
    267, 10893.29, 8967.67, 7773.35, 5773.49, 7856.12, -1.0
    595, 1949.30, 985.77, 445.34, 9565.31, 6249.56, 7256.93, -1.0
    .
    .
    .

  2. #2
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Reading comma delimited file

    The information you put in the code window is your csv file. Can you show us your code so we can help you correct it?
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Reading comma delimited file

    What you're showing us is a bunch of numbers... How can we possibly know what each line stands for, and what each number in each column stands for? If you just want to read this data into an array, and since the number of fields in each line differs, you can use a 2-d jagged array. However, a regular 2-d array will work too. Use tream reader to open the text file, the read each line and parse it into and array. Search for reading csv file in this forum and you will be able to get something to help you started.

  4. #4
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Reading comma delimited file

    Stan,

    The original post tells you exactly what the lines of numbers are, which is why I only asked to see the code. The first number is the ID, the remaining numbers are sales figures and the EOL.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  5. #5
    Lively Member
    Join Date
    Jun 2005
    Location
    New Jersey, USA
    Posts
    119

    Re: Reading comma delimited file

    you have a csv file? why dont you make a data connection to the excel document?

    Or, if you want to do it by reading the file directly:

    VB Code:
    1. Private Sub ReadFile()
    2.         Dim myfile As String = "yourCSVfilename.csv"
    3.         Dim x() As arraylist = buildarrays(myfile)
    4.         Dim ID() = x(0).ToArray
    5.         Dim totalsales() = x(1).ToArray
    6.  
    7.         'Now you have your arrays filled with data, do with them as you wish.
    8.     End Sub
    9.  
    10.     Private Function BuildArrays(ByVal sourcefile As String)
    11.         Dim a As Integer
    12.         Dim ID As New arraylist
    13.         Dim totalsales As New arraylist
    14.         Dim reader As New IO.Streamreader(sourcefile)
    15.         Do While reader.endofstream = False
    16.             Dim line As String = reader.readline()
    17.             Dim contents() As String = split(line, ", ")
    18.             ID.Add(contents(0))
    19.             Dim b As Integer
    20.             Dim totalsalesforline As Single = 0
    21.             For b = 1 To ubound(contents) - 1
    22.                 totalsalesforline = totalsalesforline + CSng(contents(b))
    23.             Next
    24.             totalsales.Add(totalsalesforline)
    25.         Loop
    26.         reader.close()
    27.         Dim x(1) As arraylist
    28.         x(0) = ID
    29.         x(1) = totalsales
    30.         Return x
    31.     End Function

    This should work.
    Dim person As New Person
    Person.GrowUp(ByVal school as string, ByVal gang as string, Byval family as string)
    Person.Work
    Dim A as Integer
    For A = 1 to 10
    Person.Marry()
    Person.Divorce()
    Next
    Person.GiveUp()
    Person.Die()

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    4

    Re: Reading comma delimited file

    Here's what I have tried, but errors are found on the line "ID(x) = CInt(Sales(0))"


    VB Code:
    1. Dim ID() As Integer
    2.     Dim totalsales() As Double    
    3. Sub Input()
    4.         Dim sr As IO.StreamReader = IO.File.OpenText("sales.txt")
    5.         Dim Sales() As String
    6.         Dim i As Integer
    7.         Dim x As Integer = -1
    8.  
    9.         Do Until (sr.Peek = -1)
    10.             Dim line As String = sr.ReadLine
    11.             Sales = line.Split(","c)
    12.             x += 1
    13.             ID(x) = CInt(Sales(0))
    14.             For i = 1 To Sales.GetUpperBound(0) - 1
    15.                 totalsales(x) += CDbl(Sales(i))
    16.             Next
    17.  
    18.         Loop
    19. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    20.         Input()
    21.    End Sub

  7. #7

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    4

    Re: Reading comma delimited file

    Thanks colonel720, I'll try that later when I get home.
    Meanwhile, could anybody fix my codes? Thanks a lot!

  8. #8
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Reading comma delimited file

    This should do it:
    VB Code:
    1. 'Class level variables
    2.     Private ID(0) As Integer
    3.     Private totalsales(0) As Double
    4.  
    5.     Private Sub ReadData()
    6.         Dim sr As IO.StreamReader = IO.File.OpenText("C:\test.txt") 'Change the file path as needed
    7.         Dim fields() As String
    8.         Dim i As Integer = 0
    9.         Dim x As Double = 0D
    10.         Dim total As Double = 0D
    11.         Do Until (sr.Peek = -1)
    12.             fields = sr.ReadLine.Split(","c)
    13.             If fields.Length > 1 Then
    14.                 ReDim Preserve ID(i)
    15.                 ReDim Preserve totalsales(i)
    16.                 ID(i) = fields(0)
    17.                 For j As Integer = 1 To fields.GetUpperBound(0) - 1
    18.                     total += Double.Parse(fields(j))
    19.                 Next
    20.                 totalsales(i) = total
    21.                 i += 1
    22.             End If
    23.         Loop
    24.         sr.Close()
    25.     End Sub
    To run the code, you just put under a button click or something this code
    VB Code:
    1. ReadData()
    2.         Dim str As String
    3.         For i As Integer = 0 To ID.GetUpperBound(0)
    4.             str &= "Total sales for Salesperson " & ID(i).ToString & " is: " & totalsales(i).ToString() & vbCrLf
    5.         Next
    6.         MsgBox(str)

  9. #9

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    4

    Re: [RESOLVED] Reading comma delimited file

    that worked
    thank you very much

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