CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2004

    Reading a random line from a text file

    How do I read a random line of text, from a text document.

    text File example:

    this is line one!
    this is line two!
    this is line three!
    this is line four!
    this is line five!

    How would I assign a RANDOM one of those lines to a string or a caption?
    Last edited by EnEsCe; November 10th, 2004 at 11:57 PM.

  2. #2
    Join Date
    Nov 2004

    Re: Reading a random line from a text file

    I'm not sure what you mean by random line. If you really mean random line then you can tell your app to skip a randomized number of lines, then read it into a string vlaue and assign it to the caption. That doesn't make sense to me, but it's what I make out of your post.

  3. #3
    Join Date
    Aug 2004

    Reading a random line from a text file

    Well? Somone smart help me unlike the dude above me...

  4. #4
    Join Date
    Sep 2001
    Québec, Canada

    Re: Reading a random line from a text file

    You can read a line with the Line Input # instruction. If you want to read a random line, you need first to know how much line there is in your text, this could be done by pre-saving the number of lines in a registry key or on the first line in the text file. Then, just use the RND function to generate a random line number and read line by line until you get to the line.

    Another option would be to read ALL the text file into a single string, use the Split() function and then select a random line (see our FAQ for example: http://www.codeguru.com/forum/showthread.php?t=220033), this could be used if the file is small and if you will need to read multiple lines from the text file (the first method will take lot of time as it need to read the text line by line everytime)

    Hope it helps

    CodeGuru VB FAQ Visual Basic Frequently Asked Questions
    VB Code color Tool to color your VB code on CodeGuru
    Before you post Importants informations to know before posting

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.