Results 1 to 13 of 13

Thread: reading a text file

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Location
    Brisbane, Australia
    Posts
    12

    Unhappy reading a text file

    hey

    when u open a text file to read u use lineinput(*Varibal*).

    when i use a post tested loop such as

    do

    username=lineinput(filenum)
    password=lineinput(filenum)

    loop until EOF or lineinput(filenum)=""


    that then will read 3 lines each time it loops and by that then some lines will be skipped. the reason i have that lineinput(filenum)="" is because some times in textfiles u get extra unwanted lines which get read. what i am asking is how can u make it so that it still reads the 2 lines but when it comes to check the line it doesn't proceed to the next line, so that when it loops the data will be read properly.

    so can anyone help me
    In a world of terror lives a world of fear!

  2. #2
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    Re: reading a text file

    Try using the StreamReader. That object allows you to ReadLine().

    What object are you using to open the file and read it?

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: reading a text file

    As Aspnot says, use a StreamReader to read the data from the file. You would then use the Peek method to determine whether there is still data to be read, which checks for data but does not actually read it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Location
    Brisbane, Australia
    Posts
    12

    Re: reading a text file

    I am using a click event on a button
    In a world of terror lives a world of fear!

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: reading a text file

    Quote Originally Posted by Aphid2005
    I am using a click event on a button
    That makes no difference. You can put whatever code you want wherever you want. You can use a StreamReader in the Button's Click event handler or you can put it in a method that you call from the event handler.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    New Member
    Join Date
    Mar 2005
    Posts
    1

    Re: reading a text file

    Dim sr As IO.StreamReader =IO.File.OpenText("nameoffile.txt)

    Do while sr.Peek <> - 1
    username = sr.ReadLine()
    password = sr.ReadLine()
    Loop

    The only problem with StreamReader is that you cannot choose which line you want to read when using the ReadLine method. It has always got to go line by line.

  7. #7
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: reading a text file

    Make an If Condition inside your loop to evaluate what the line is, and execute the code you want...
    VB Code:
    1. Do while sr.Peek <> - 1
    2.      Dim strLine as String
    3.      strLine = sr.ReadLine()
    4.      If strLine = "" then
    5.            'do nothing
    6.      Else
    7.            'add more code or If statements to evaluate the string further....
    8.            username = strLine    'just an example
    9.      End If
    10. Loop

  8. #8
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: reading a text file

    or simplify gigemboy's code:

    VB Code:
    1. Do while sr.Peek <> - 1
    2.      Dim strLine as String = sr.ReadLine()
    3.      If strLine <> "" then
    4.            username = strLine    'just an example
    5.      End If
    6. Loop

  9. #9
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: reading a text file

    Juggalo, won't that create and dispose the variable strline every time?

    HEre's how I do it..
    VB Code:
    1. Dim sr As New streamreader(filename)
    2. Dim line As String
    3.  
    4. Do
    5.      line = sr.readline
    6.      'Check, store, manipulate etc variable "line"
    7. Loop Until line Is Nothing
    8. sr.Close()
    9. line.Dispose()

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  10. #10
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: reading a text file

    It won't dispose it, the code works, and you really don't have to call your line.dispose in your code, conipto, since you declared it with the "Dim" statement. The line is automatically disposed once it is out of scope...
    Last edited by gigemboy; Oct 22nd, 2005 at 08:28 PM.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: reading a text file

    You can declare the string variable outside the loop to avoid a new variable being created each iteration, but note that the String class does not have a Dispose method, so you just leave it up to the GC to clean up the managed resources when its ready.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Location
    Brisbane, Australia
    Posts
    12

    Re: reading a text file

    thanks guys
    In a world of terror lives a world of fear!

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: reading a text file

    Don't forget to resolve your thread from the Thread Tools menu.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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