Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 6th, 2005, 2:13 PM   #1
cloud-
Hobbyist Programmer
 
Join Date: Jan 2005
Posts: 110
Rep Power: 4 cloud- is on a distinguished road
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
cloud- is offline   Reply With Quote
Old Apr 6th, 2005, 2:40 PM   #2
chepfaust
Programmer
 
chepfaust's Avatar
 
Join Date: Feb 2005
Posts: 62
Rep Power: 4 chepfaust is on a distinguished road
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.
chepfaust is offline   Reply With Quote
Old Apr 6th, 2005, 4:20 PM   #3
Rory
Expert Programmer
 
Rory's Avatar
 
Join Date: Jan 2005
Location: London
Posts: 542
Rep Power: 4 Rory is on a distinguished road
Send a message via MSN to Rory
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))
Hope this helps.
Rory is offline   Reply With Quote
Old Apr 6th, 2005, 11:16 PM   #4
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Apr 7th, 2005, 6:08 AM   #5
cloud-
Hobbyist Programmer
 
Join Date: Jan 2005
Posts: 110
Rep Power: 4 cloud- is on a distinguished road
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?
or does that read one line at a time?


thanks for the help
cloud- is offline   Reply With Quote
Old Apr 7th, 2005, 9:51 AM   #6
chepfaust
Programmer
 
chepfaust's Avatar
 
Join Date: Feb 2005
Posts: 62
Rep Power: 4 chepfaust is on a distinguished road
Quote:
Originally Posted by cloud-
hi thanks for the replys
but i dont understand how i can use what rory said

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?
or does that read one line at a time?


thanks for the help
better than line input; what rory did was use the split() function to chop up the entire text file and put each individual line into an array called "mylines."

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))
chepfaust is offline   Reply With Quote
Old Apr 7th, 2005, 11:29 AM   #7
cloud-
Hobbyist Programmer
 
Join Date: Jan 2005
Posts: 110
Rep Power: 4 cloud- is on a distinguished road
But how do i get the textfile in that variable (MyString)?

thanks alot ^^
cloud- is offline   Reply With Quote
Old Apr 7th, 2005, 1:31 PM   #8
Rory
Expert Programmer
 
Rory's Avatar
 
Join Date: Jan 2005
Location: London
Posts: 542
Rep Power: 4 Rory is on a distinguished road
Send a message via MSN to Rory
Quote:
Originally Posted by Infinite Recursion
Rory, I declare you as being the resident VB wizard
Thanks! (I hope to learn a proper language some day)

Open "C:\AFile.txt" For Input As #1
    MyString = Input(LOF(1), 1)
Close #1

That 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
Rory is offline   Reply With Quote
Old Apr 7th, 2005, 2:43 PM   #9
cloud-
Hobbyist Programmer
 
Join Date: Jan 2005
Posts: 110
Rep Power: 4 cloud- is on a distinguished road
ahh tx ^^
but i get an error on :
RandomLine = MyLines(Fix((Ubound(MyLines) + 1) * Rnd))

saying expected array..
any ideas?

thanks alot
cloud- is offline   Reply With Quote
Old Apr 7th, 2005, 6:39 PM   #10
Rory
Expert Programmer
 
Rory's Avatar
 
Join Date: Jan 2005
Location: London
Posts: 542
Rep Power: 4 Rory is on a distinguished road
Send a message via MSN to Rory
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))
Rory is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:23 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC