Help Please, how do i read from text file into array?

Reply

Join Date: Feb 2007
Posts: 1
Reputation: Iaintnofool is an unknown quantity at this point 
Solved Threads: 0
Iaintnofool Iaintnofool is offline Offline
Newbie Poster

Help Please, how do i read from text file into array?

 
0
  #1
Feb 10th, 2007
Hi I'm stuck on a project using VB 2005 express edition. I'm having trouble reading in data into my code from a Text File.
I'm using an openFileDialog so the user can select a file to open. The opened file will contain 3 columns and an unknown number of rows. I want to read the data from the file calling the first colum x, second y, and third z. I then want to plot a graph of the raw data (x,y). Before performing simple mathematical equations on each row of x,y and z and sum the answers.
Unfortunately so far I can't get past inputing the data into the program, so I'd be very grateful for any help you can give me.
  1. Public Class Form1
  2. Dim data(i, 2) As String
  3. Dim g, filename As String
  4. Dim i, numlines, lines, As Integer
  5. Dim x(i), y(i), s(i) As Double
  6. Private Sub OpenFileBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenFileBtn.Click
  7. If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then 'opens file of users choice if cancel not pressed
  8. FileOpen(1, OpenFileDialog1.FileName, OpenMode.Input)
  9. lines = getNumLines(1)
  10. FileClose(1)
  11. FileOpen(1, OpenFileDialog1.FileName, OpenMode.Input)
  12. ReDim data(lines, 2)
  13. For i = 0 To i = lines - 1
  14. Input(1, data(i, 0))
  15. Input(1, data(i, 1))
  16. Input(1, data(i, 2))
  17. x(i) = Val(data(i, 0))
  18. y(i) = Val(data(i, 1))
  19. s(i) = Val(data(i, 2))
  20. Graph1.Plotxy(x(i), y(i))
  21. Next i
  22. FileClose(1)
  23. End If
  24. End Sub
  25. Public Function getNumLines(ByVal fileName As Integer) As Integer
  26. Dim numLines As Integer
  27. numLines = 0
  28. While Not (EOF(1))
  29. numLines = numLines + 1 'counts number of rows in the file
  30. Input(1, g)
  31. End While
  32. Return numLines 'return the number of lines found
  33. End Function
  34. End Class
Last edited by WaltP; Feb 11th, 2007 at 3:02 am. Reason: Added code tags so we can read your code
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC