|
-
May 4th, 2005, 04:45 AM
#1
Thread Starter
Junior Member
a file and basic maths
Im in the middle of writing a program. Part of that prog is to open a .txt file . The format of the file is two colums of numbers seperated by a comma i.e. row one would look like 0.23657, -3.6548,
approximately 3000 rows will be in the file.
I need to read in the data from the file, perform some maths and output the result.
Something like get the average of column 1 and the average from column 2 and add these results together.
Any ideas or source code?
Many thanx, Paul...
-
May 4th, 2005, 04:50 AM
#2
Re: a file and basic maths
I would use a 2 dimensional array, and a principal like pixels.
Eg:
VB Code:
Dim tTable(1, 2999) As String 'might take a while to load
'X 'Y
Dim inptLine As String 'the input
Open "C:\test.txt" For Input As #1
Do While Not EOF(1)
Input #1, inptLine
For i = 0 To 2999
For j = 0 To 1
tTable(j, i) = inptLine
Next j
Next i
Loop
Close #1
Then you could use something like:
VB Code:
Dim avg As Double
For i = 0 To 2999
avg = avg + tTable(0, i)
Next i
avg = (avg / 2999)
Unfortunately, if you are using text files, there won't be a quick way for this..
Phreak
Visual Studio 6, Visual Studio.NET 2005, MASM
-
May 4th, 2005, 06:18 AM
#3
Frenzied Member
Re: a file and basic maths
You can split the lines with Output = Split(Input, Delimiter).
Output will be an array.
IIRC, when converting strings to numbers VB uses "," as the decimal separator.
You can replace the "." with Output = Replace(Input, ToReplace, ReplaceWith).
-
May 4th, 2005, 06:42 AM
#4
Re: a file and basic maths
But why split?
Visual Basic automatically delimits by commas, and since thats how the file is layed out, 1 input statement would input everything up to either a new line character, or a comma (,).
Phreak
Visual Studio 6, Visual Studio.NET 2005, MASM
-
May 4th, 2005, 09:00 AM
#5
Thread Starter
Junior Member
Re: a file and basic maths
Im gonna try The "phreak" code.
Just one thing..... tTable is just like a 2d array, yes?
-
May 4th, 2005, 09:41 AM
#6
Re: a file and basic maths
 Originally Posted by jeroen79
IIRC, when converting strings to numbers VB uses "," as the decimal separator.
You can replace the "." with Output = Replace(Input, ToReplace, ReplaceWith).
When converting strings to numbers, VB will use the seperator that is defined in your regional settings.
-
May 7th, 2005, 07:54 AM
#7
Thread Starter
Junior Member
Re: a file and basic maths
i just realised that the above code wont work the way i want it to.
i need to have both the variables in the row to be read in together. i.e. i need to have the x and y values of row one etc at the same time. (part of my formula needs to ultiply the x and y value of a row together. then acculmulate them.) (i know how to do the maths...i just need the two variables read together)
i know i can do this with ttable but it will require at least another loop. id like to do the calculations as the data is being read, in the same loop.
-
May 7th, 2005, 08:04 AM
#8
Frenzied Member
Re: a file and basic maths
You could do two Inputs each time it goes through the loop and send the result of each Inout to a different variable.
-
May 7th, 2005, 08:45 AM
#9
Thread Starter
Junior Member
Re: a file and basic maths
so how do i do that?
(and bty the number of rows will be approx 3000 but the exact number in the file will be unknown)
-
May 7th, 2005, 10:35 AM
#10
Thread Starter
Junior Member
Re: a file and basic maths
ok all i got it sorted...... heres the portion of code im using to do that....its a bit messy but its does the job nicely..... (i added in a few RTB's for trouble shooting)
ill be adding in my maths later.
it also is much quicker than i thought. reads in 6000 values via 2 loops and displays them in 2 seconds on a p3.
VB Code:
Private Sub Command3_Click()
Open "C:\Documents and Settings\paul.cullen\Desktop\log.txt" For Input As #1
While Not EOF(1)
Line Input #1, temp$ 'read in one line at a time, and put in temp$
alltext$ = alltext$ & temp$ & (Chr(13)) 'append to alltext$ with line feed. (just for trouble shooting)
For I = 1 To Len(temp$) - 1 'the minus one is so that the loop will not read the second comma in the file and repeat for no reason.
y = Mid(temp$, I, 1) 'checking for comma
'n = n + 1 'acculuminating number of characters before comma is reached
If y = "," Then 'check for commas
x$ = Left(temp$, I - 1) ' at the first comma, this copies all the characters from the left to x$
allx = allx & x$ & (Chr(13)) '(just for trouble shooting)
ys$ = Right(temp$, Len(temp$) - I) ' taking all the characters to the right of the comma
ys$ = Left(ys$, Len(temp$) - I - 1) ' removeing the comma at the end of the "y" data
ally = ally & ys$ & (Chr(13)) '(just for trouble shooting)
End If
Next
Wend
RichTextBox1 = alltext$ 'temp read viewer. all RTB'sare for troubleshooting purposes.....removes them as needed.
RichTextBox2 = ally
RichTextBox3 = allx
Close #1
End Sub
-
May 7th, 2005, 12:48 PM
#11
Frenzied Member
Re: a file and basic maths
How about:
VB Code:
Input #1, A
Input #1, B
DoStuff (A, B)
And if want to split a Line Input result on the comma then Split() would be easier.
Last edited by jeroen79; May 7th, 2005 at 12:51 PM.
-
May 9th, 2005, 05:33 AM
#12
Thread Starter
Junior Member
Re: a file and basic maths
looks nice (and way easier)but iv it done my way now. i had to do this for a couple of files so im not gonna change it now but thanks for the help anyway. im sure ill use it in future programming.
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
|