Oct 21st, 2001, 09:35 AM
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.
Oct 21st, 2001, 09:44 AM
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?
(Fighting with the RightToLeft bugs in VS 2005)
Oct 21st, 2001, 09:49 AM
im afraid i didnt understand any of the above so if you could maybe show some examples that would be great
Oct 21st, 2001, 12:17 PM
Dim strFile() As String
Dim sFile As String
Open "MyFile" For Input As #1
sFile = Input(LOF(1), 1)
strFile = Split(sFile, vbCrLf)
For I = 1 To 10
nLine = Int(Rnd * UBound(sFile))
text1 = text1 & strFile(nLine) & vbCrLf
Oct 21st, 2001, 01:37 PM
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.
Oct 21st, 2001, 04:18 PM
Oct 21st, 2001, 04:58 PM
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?
Oct 21st, 2001, 05:04 PM
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.
Oct 21st, 2001, 05:34 PM
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
Dim intFF As Long
Dim strLine As String
Dim intLine As Long
Dim intLines As Long
Dim intCurrent As Long
intFF = FreeFile
intLines = 0
' get number of lines in file
Open "c:\winnt\win.ini" For Input As #intFF
Do While Not EOF(intFF)
Line Input #intFF, strLine
intLines = intLines + 1
'now we know number of lines, we can generate a random number
intLine = Int((intLines - 1 + 1) * Rnd + 1)
'we now want line# intline
Seek #intFF, 1
intCurrent = 1
Do While intCurrent <= intLine
Line Input #intFF, strLine
intCurrent = intCurrent + 1
MsgBox "Line # " & intLine & " contains: " & Chr(34) & strLine & Chr(34)
Nov 7th, 2013, 11:10 AM
Re: Read Random Lines From Text File
thanks for the code. it works.
Click Here to Expand Forum to Full Width
Survey posted by VBForums.