-
Oct 21st, 2001, 08:35 AM
#1
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, 08:44 AM
#2
Fanatic Member
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)
-
Oct 21st, 2001, 08:49 AM
#3
im afraid i didnt understand any of the above so if you could maybe show some examples that would be great
Thanks alot
-
Oct 21st, 2001, 11:17 AM
#4
Try this
VB Code:
Dim strFile() As String
Dim sFile As String
Open "MyFile" For Input As #1
sFile = Input(LOF(1), 1)
strFile = Split(sFile, vbCrLf)
Close #1
For I = 1 To 10
Randomize Timer
nLine = Int(Rnd * UBound(sFile))
text1 = text1 & strFile(nLine) & vbCrLf
Next I
-
Oct 21st, 2001, 12:37 PM
#5
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, 03:18 PM
#6
Addicted Member
-
Oct 21st, 2001, 03:58 PM
#7
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, 04:04 PM
#8
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
-
Oct 21st, 2001, 04:34 PM
#9
PowerPoster
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:
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
Loop
'now we know number of lines, we can generate a random number
Randomize
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
Loop
Close #intFF
MsgBox "Line # " & intLine & " contains: " & Chr(34) & strLine & Chr(34)
-
Nov 7th, 2013, 11:10 AM
#10
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|