![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Jan 2005
Posts: 110
Rep Power: 4
![]() |
reading a random line from a text file
how would i find out the number of lines in a textfile and
i also need to read a random line from the text file.. any ideas thanks |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Feb 2005
Posts: 62
Rep Power: 4
![]() |
LOF(#x) will tell you the length of the textfile, where the x is the number you open the file as.
dunno about reading a random line though. |
|
|
|
|
|
#3 |
|
Expert Programmer
|
Right, you can think about a line as being defined by a line break. Thus the number of lines in a string or text file is given by Ubound(Split(MyString,VbNewLine)) + 1.
Or more usefully, a random line is: Dim MyLines() as String Dim RandomLine as String Randomize(Timer) MyLines = Split(MyString,vbNewLine) RandomLine = MyLines(Fix((Ubound(MyLines) + 1) * Rnd)) |
|
|
|
|
|
#4 |
|
Programming Guru
![]() ![]() ![]() |
Rory, I declare you as being the resident VB wizard
![]()
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Jan 2005
Posts: 110
Rep Power: 4
![]() |
hi thanks for the replys
![]() but i dont understand how i can use what rory said Function RndGetLine(Section As Integer) As String Dim FsFile As String FsFile = FreeFile Open "C:\Cloud\test4.text" For Input As #FsFile ...?? ..?? End Function do i have to read the whole text file to a variable for your code to work how do i do that? Line Input #Fsfile, variable? thanks for the help |
|
|
|
|
|
#6 | |
|
Programmer
Join Date: Feb 2005
Posts: 62
Rep Power: 4
![]() |
Quote:
MyLines = Split(MyString,vbNewLine) then all you have to do is generate a random number up to the upper bound of the array and display the corresponding line of the array (which displays that line of the text file!). in this case the random line gets stored to the string "randomline." RandomLine = MyLines(Fix((Ubound(MyLines) + 1) * Rnd)) |
|
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Jan 2005
Posts: 110
Rep Power: 4
![]() |
But how do i get the textfile in that variable (MyString)?
thanks alot ^^ |
|
|
|
|
|
#8 | |
|
Expert Programmer
|
Quote:
Open "C:\AFile.txt" For Input As #1
MyString = Input(LOF(1), 1)
Close #1That will load a unicode file into the variable MyString. I do it in binary mode like this, but there are probably more elegant ways. Dim MyString As String
Dim MyBuffer() As Byte
Open "C:\AFile.txt" For Binary Access Read As #1
ReDim MyBuffer(LOF(1) - 1)
Get 1, , MyBuffer
MyString = StrConv(MyBuffer, vbUnicode)
ReDim MyBuffer(0) 'Free up memory
Close #1 |
|
|
|
|
|
|
#9 |
|
Hobbyist Programmer
Join Date: Jan 2005
Posts: 110
Rep Power: 4
![]() |
ahh tx ^^
but i get an error on : RandomLine = MyLines(Fix((Ubound(MyLines) + 1) * Rnd)) saying expected array.. any ideas? thanks alot |
|
|
|
|
|
#10 |
|
Expert Programmer
|
Is there anything in Mylines?
The full code would be: Dim MyLines() as String Randomize(Timer) Open "C:\AFile.txt" For Input As #1 MyLines = Split(Input(LOF(1), 1),vbNewLine) Close #1 RandomLine = MyLines(Fix((Ubound(MyLines) + 1) * Rnd)) |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|