DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Help Please, how do i input from text file?

  1. #1
    Join Date
    Feb 2007

    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)

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

  2. #2
    Join Date
    Oct 2005
    How are those columes are represented / separeted in the file ?
    like this for example :

    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 .
    Programmer&Cracker CS

  3. #3
    Join Date
    Nov 2003
    Portland, OR
    Please post a few lines of the text file.
    Phil Weber

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  4. #4
    Join Date
    Feb 2007

    A few lines of the data

    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

  5. #5
    Join Date
    Oct 2005


    '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] .
    Last edited by Amahdy; 02-11-2007 at 11:04 AM. Reason: Add more info.
    Programmer&Cracker CS

Similar Threads

  1. writing to a text file
    By persian_celina in forum .NET
    Replies: 0
    Last Post: 02-19-2006, 03:06 AM
  2. Replies: 1
    Last Post: 01-12-2006, 07:42 PM
  3. Replies: 1
    Last Post: 03-23-2005, 12:18 AM
  4. NullPointerException when reading text file
    By Andrew McLellan in forum Java
    Replies: 3
    Last Post: 05-09-2001, 05:34 PM
  5. Replies: 0
    Last Post: 04-17-2000, 01:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center