Read Random Lines From Text File-VBForums
Results 1 to 10 of 10

Thread: Read Random Lines From Text File

  1. #1
    Hellstorm
    Guest

    Read Random Lines From Text File

    Hey does anyone know how when i calick a button on my form it will read a random line from a text file that i created and insert it in to a text box?

    Thanks For Any Help.

  2. #2
    Fanatic Member BrianHawley's Avatar
    Join Date
    Aug 2001
    Location
    Saudi Arabia
    Posts
    796
    Is your file small enough to fit it all into memory at the same time?

    If so, load each line of your file into an array.

    Generate a random number between 0 and the last element of the array.

    That's your text!

    If you don't want repeats, set the array element to null after you use it. (If you pick a null in future, guess again.)

    If your file is too big to fit in memory, open it and get the length. Generate a random number less than the length of the file. Go there. Go forward until you hit a chr$(13) (or run off the end, in which case go to the start. Read from there util the next chr$(13) (or the end). That's your string.

    Any bits of that you need the code for, or is the principle enough?
    Brian
    (Fighting with the RightToLeft bugs in VS 2005)

  3. #3
    Hellstorm
    Guest
    im afraid i didnt understand any of the above so if you could maybe show some examples that would be great

    Thanks alot

  4. #4
    Megatron
    Guest
    Try this
    VB Code:
    1. Dim strFile() As String
    2. Dim sFile As String
    3.  
    4. Open "MyFile" For Input As #1
    5.     sFile = Input(LOF(1), 1)
    6.     strFile = Split(sFile, vbCrLf)
    7. Close #1
    8.  
    9. For I = 1 To 10
    10.     Randomize Timer
    11.     nLine = Int(Rnd * UBound(sFile))
    12.     text1 = text1 & strFile(nLine) & vbCrLf
    13. Next I

  5. #5
    Hellstorm
    Guest
    OK Thanks alot i tried that but it say "expected array" on the word "UBound" could you tell me what this means and what i have to do to fix the problem please.

  6. #6
    Addicted Member
    Join Date
    Oct 2000
    Posts
    137
    Try changing UBound(sFile) to UBound(strFile)

    Hope it workz!

    Thanx,
    Mikelo2k

  7. #7
    Hellstorm
    Guest
    Thanks alot for the help that all works except it reads a few lines instead of just one. Does anyone know how to let it know the different lines?

  8. #8
    Helger
    Guest
    Although a lot of people don't like the filesystemobject I'd suggest you have a look into the fso's textstream-object. You will have much less to worry about and easier control. Need to have a reference to the microsoft scripting runtime which will use a few kb of diskspace and memory, but oh well neither is a very rare ressource.

    Helger

  9. #9
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    Try this. It should be a lot more memory efficient that Megatron's example (esp with large files) as it only ever stores 1 line instead of the whole file
    VB Code:
    1. Dim intFF As Long
    2. Dim strLine As String
    3. Dim intLine As Long
    4. Dim intLines As Long
    5. Dim intCurrent As Long
    6.  
    7. intFF = FreeFile
    8. intLines = 0
    9.  
    10. ' get number of lines in file
    11. Open "c:\winnt\win.ini" For Input As #intFF
    12. Do While Not EOF(intFF)
    13.     Line Input #intFF, strLine
    14.     intLines = intLines + 1
    15. Loop
    16.  
    17. 'now we know number of lines, we can generate a random number
    18. Randomize
    19. intLine = Int((intLines - 1 + 1) * Rnd + 1)
    20.  
    21. 'we now want line# intline
    22. Seek #intFF, 1
    23. intCurrent = 1
    24.  
    25. Do While intCurrent <= intLine
    26.     Line Input #intFF, strLine
    27.     intCurrent = intCurrent + 1
    28. Loop
    29. Close #intFF
    30.  
    31. MsgBox "Line # " & intLine & " contains: " & Chr(34) & strLine & Chr(34)

  10. #10
    Member
    Join Date
    Aug 2013
    Posts
    47

    Re: Read Random Lines From Text File

    thanks for the code. it works.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.