Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Help Please, how do i input from text file?

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.

Public Class Form1
Dim data(i, 2) As String
Dim g, filename As String
Dim i, numlines, lines, As Integer
Dim x(i), y(i), s(i) As Double

Private Sub OpenFileBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenFileBtn.Click
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then 'opens file of users choice if cancel not pressed
FileOpen(1, OpenFileDialog1.FileName, OpenMode.Input)
lines = getNumLines(1)
FileClose(1)

FileOpen(1, OpenFileDialog1.FileName, OpenMode.Input)
ReDim data(lines, 2)
For i = 0 To i = lines - 1

Input(1, data(i, 0))
Input(1, data(i, 1))
Input(1, data(i, 2))
x(i) = Val(data(i, 0))
y(i) = Val(data(i, 1))
s(i) = Val(data(i, 2))
Graph1.Plotxy(x(i), y(i))
Next i
FileClose(1)
End If
End Sub

Public Function getNumLines(ByVal fileName As Integer) As Integer

Dim numLines As Integer

numLines = 0

While Not (EOF(1))
numLines = numLines + 1 'counts number of rows in the file
Input(1, g)
End While

Return numLines 'return the number of lines found

End Function
End Class
[2196 byte] By [Inneedofhelp] at [2007-11-11 10:22:26]
# 1 Re: Help Please, how do i input from text file?
How are those columes are represented / separeted in the file ?
like this for example :
x1,y1,z1
x2,y2,z2
x3,y3,z3
...

or separeted by space or what ?
each input take one complete line, so your first code loop will have results :
x(0) = x1 y1 z1
y(0) = x2 y2 z2
z(0) = x3 y3 z3

and of course this is not what u want , so u may use the "split()" function or any parsing method to solve this .
Otherwise tell us how is your text format is .
Amahdy at 2007-11-11 20:48:02 >
# 2 Re: Help Please, how do i input from text file?
Please post a few lines of the text file.
Phil Weber at 2007-11-11 20:49:04 >
# 3 Re: Help Please, how do i input from text file?
1.0000 20.655 1.0000
2.0000 20.438 1.0000
3.0000 20.958 1.0000
4.0000 20.292 1.0000
5.0000 20.611 1.0000
Inneedofhelp at 2007-11-11 20:50:00 >
# 4 Re: Help Please, how do i input from text file?
'make the data() array one dimension only
dim tmp() as string
For i = 0 To i = lines - 1
Input(1, data(i))
data(i) = mid(data(i),3) 'start from the third position to remove the "1." , "2." , ... at the line begin ;
tmp() = split(data(i), " ")
'Impotant (if Ubound(tmp) = -1 then RAISE_ERROR_OR_SKIP_THIS_LOOP)
x(i) = Val(tmp(0))
y(i) = Val(tmp(1))
s(i) = Val(tmp(2))
Graph1.Plotxy(x(i), y(i))
Erase tmp()
Next i

I think -but not sure- if the "data()" type was Integer , your first code will work exept that u must first elimenate the line begin [1. , 2., 3., ... etc] .
Amahdy at 2007-11-11 20:51:05 >