Results 1 to 12 of 12

Thread: a file and basic maths

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    21

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

  2. #2
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: a file and basic maths

    I would use a 2 dimensional array, and a principal like pixels.
    Eg:
    VB Code:
    1. Dim tTable(1, 2999) As String 'might take a while to load
    2.               'X   'Y
    3. Dim inptLine As String 'the input
    4. Open "C:\test.txt" For Input As #1
    5. Do While Not EOF(1)
    6.     Input #1, inptLine
    7.     For i = 0 To 2999
    8.         For j = 0 To 1
    9.             tTable(j, i) = inptLine
    10.         Next j
    11.     Next i
    12. Loop
    13. Close #1
    Then you could use something like:
    VB Code:
    1. Dim avg As Double
    2. For i = 0 To 2999
    3.     avg = avg + tTable(0, i)
    4. Next i
    5. 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

  3. #3
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

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

  4. #4
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    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

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    21

    Re: a file and basic maths

    Im gonna try The "phreak" code.
    Just one thing..... tTable is just like a 2d array, yes?

  6. #6
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: a file and basic maths

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

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    21

    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.

  8. #8
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    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.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    21

    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)

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    21

    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:
    1. Private Sub Command3_Click()
    2.  Open "C:\Documents and Settings\paul.cullen\Desktop\log.txt" For Input As #1
    3.  
    4.  
    5.     While Not EOF(1)
    6.  
    7.         Line Input #1, temp$               'read in one line at a time, and put in temp$
    8.         alltext$ = alltext$ & temp$ & (Chr(13)) 'append to alltext$ with line feed. (just for trouble shooting)
    9.  
    10.    
    11.         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.
    12.                                      
    13.             y = Mid(temp$, I, 1)       'checking for comma
    14.             'n = n + 1                  'acculuminating number of characters before comma is reached
    15.             If y = "," Then             'check for commas
    16.                x$ = Left(temp$, I - 1)  ' at the first comma, this copies all the characters from the left to x$
    17.                allx = allx & x$ & (Chr(13)) '(just for trouble shooting)
    18.                ys$ = Right(temp$, Len(temp$) - I) ' taking all the characters to the right of the comma
    19.                ys$ = Left(ys$, Len(temp$) - I - 1) ' removeing the comma at the end of the "y" data
    20.                ally = ally & ys$ & (Chr(13)) '(just for trouble shooting)
    21.             End If
    22.            
    23.         Next
    24.     Wend
    25.     RichTextBox1 = alltext$ 'temp read viewer. all RTB'sare for troubleshooting purposes.....removes them as needed.
    26.     RichTextBox2 = ally      
    27.     RichTextBox3 = allx
    28.  
    29. Close #1
    30. End Sub

  11. #11
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    Re: a file and basic maths

    How about:
    VB Code:
    1. Input #1, A
    2.   Input #1, B
    3.   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.

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    21

    Thumbs up 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
  •  



Click Here to Expand Forum to Full Width