Results 1 to 4 of 4

Thread: [RESOLVED] Read text file and sum a column of data

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    1

    Resolved [RESOLVED] Read text file and sum a column of data

    Hi,

    I am new to the forum and would like some guidance on how I can achieve the following.

    I would like to read data from a text file after which I want to do a simple calculation that totals one of the columns.

    I have already been able to read the text file into a text box but firstly I'm not quite sure if this method of using the text box is appropriate and secondly how do I sum a column of data?

    Thank you in advance,
    suprsnipes

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Read text file and sum a column of data

    You can connect to CSV files the same way you connect to a database and use SQL queries on them.
    If this isn't appropriate for you, your task is to read the contents of a file (use IO.StreamReader class), then split each line into tokens using String.Split method or Split function thus isolating the needed data column in it, parse the value using TryParse method to convert string tokens into numeric values, then add them up.


    Manual reading (reading C:\somefile.txt and adding up the third column of integer values with comma character as a column separator):
    vb Code:
    1. Sub ReadCSV()
    2.     Dim filecontents As String() = IO.File.ReadAllLines("c:\somefile.txt")
    3.     Dim Sum As Integer = 0 ' This holds the sum
    4.     For Each line In filecontents
    5.         ' Using comma ',' as a separator (you may use other characters)
    6.         Dim tokens() As String = line.Split(New Char() {","c})
    7.         Dim value As Integer = 0 ' This holds the current value
    8.         ' Let's suppose you need to add up a third column of integer values:
    9.         If tokens.Length >= 2 Then
    10.             ' Parsing the third column and if successful add it to the sum:
    11.             If Integer.TryParse(tokens(2), value) Then
    12.                 Sum += value
    13.             End If
    14.         End If
    15.     Next
    16.     MsgBox(Sum)
    17. End Sub

  3. #3
    New Member
    Join Date
    Oct 2014
    Posts
    1

    Re: Read text file and sum a column of data

    Thanks For Post..
    Last edited by Swati123; Oct 17th, 2014 at 08:58 AM. Reason: I corrected my answer,i found my answer

  4. #4
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: [RESOLVED] Read text file and sum a column of data

    Hi,

    Welcome to the Forum but please do not post to an old thread to ask your own question even though you may have used that thread to try and answer your question. At least you looked first before asking.

    So, please create a new thread to ask your question and just as a hint, if you compare the data types of “Sum” and “value” you will have your answer. This also implies that you have Option Strict turned Off, so turn it ON now and it will help you to eradicate these types of issues in the future.

    Hope that helps.

    Cheers,

    Ian

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