How do I read a random line of text, from a text document.
text File example:
this is line one!
this is line two!
this is line three!
this is line four!
this is line five!
How would I assign a RANDOM one of those lines to a string or a caption?
Printable View
How do I read a random line of text, from a text document.
text File example:
this is line one!
this is line two!
this is line three!
this is line four!
this is line five!
How would I assign a RANDOM one of those lines to a string or a caption?
I'm not sure what you mean by random line. If you really mean random line then you can tell your app to skip a randomized number of lines, then read it into a string vlaue and assign it to the caption. That doesn't make sense to me, but it's what I make out of your post.
Well? Somone smart help me unlike the dude above me...
You can read a line with the Line Input # instruction. If you want to read a random line, you need first to know how much line there is in your text, this could be done by pre-saving the number of lines in a registry key or on the first line in the text file. Then, just use the RND function to generate a random line number and read line by line until you get to the line.
Another option would be to read ALL the text file into a single string, use the Split() function and then select a random line (see our FAQ for example: http://www.codeguru.com/forum/showthread.php?t=220033), this could be used if the file is small and if you will need to read multiple lines from the text file (the first method will take lot of time as it need to read the text line by line everytime)
Hope it helps
JeffB