Results 1 to 4 of 4

Thread: [2008] read every line in a text file seperatly

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    [2008] read every line in a text file seperatly

    Hi everyone,

    How would i go about reading a text file from beginning to end but reading it as lines and not one huge string. For example if the text file contained 2 lines and the strings on each line were "line1" and "line2" ,respectively, how would i go about reading line1 then reacting to the value of line1 then reading line2 then reacting to it and so on. i think i could get it working if there were going to only two lines and i know what the values are but i don't. the actual values can be anything and the length in lines can be 30 (for now). any help would be great because i have been stuck on tis fro an hour. So thanks to all who can help.

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] read every line in a text file seperatly

    vb.net Code:
    1. 'declared at class level
    2. Dim myfile = My.Computer.FileSystem.OpenTextFieldParser("C:\test.txt")
    3.  
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6. ' when button1 is clicked we output the next line from our file in a message box
    7.         Messagebox.Show(myfile.ReadLine())
    8.     End Sub

    Obviously you dont have to show it in a messagebox and you dont have to have the readline() code in a button event handler but hopefully you get the idea.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: [2008] read every line in a text file seperatly

    thanks that helped. i was over complicating it. was try to do loops and if thens and stuff but this worked fine.

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] read every line in a text file seperatly

    No problem. There's other ways you can do it but thats probably the simplest.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


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