-
Feb 24th, 2011, 08:32 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Sum numbers in a textfile.
I just got a textfile with a couple of numbers (1 number in one row), how would I sum these numbers? I thought it would be easy, but thing is it's turning out to be not so easy.
-
Feb 24th, 2011, 09:29 AM
#2
Re: Sum numbers in a textfile.
Read all lines of the file to an array, loop trough the array and add the numbers...
vb.net Code:
Dim total As Double = 0 For Each line As String In IO.File.ReadAllLines("PATH_TO_FILE") total += Double.Parse(line) Next MsgBox(String.Format("Sum={0}", total.ToString), MsgBoxStyle.Information)
Last edited by mickey_pt; Feb 24th, 2011 at 09:31 AM.
Reason: Code
Rate People That Helped You
Mark Thread Resolved When Resolved
-
Feb 24th, 2011, 09:30 AM
#3
Re: Sum numbers in a textfile.
Try this:
vb.net Code:
Dim fileContents() As String = IO.File.ReadAllLines("C:\Temp\test.txt") Dim sum As Long = (From item In fileContents Select CLng(item)).Sum MsgBox(sum)
-
Feb 24th, 2011, 09:41 AM
#4
Thread Starter
Addicted Member
Re: Sum numbers in a textfile.
Both codes work perfectly, big thanks to you guys, really helped me.
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
|