Random line in a text file. [C#]
Hello,
How do you pick a random line in a text file. The text file does NOT have a set amount of lines and it will change all the time so I cannot do it my way . So i need to sort of open it and see how many lines there are and then I can take it from there. So any ideas?
Thanks alot,
Rob
[edited by - rob64464 on March 26, 2004 3:29:16 PM]
Open it, read through the entire thing and record the number of line break characters. Then, just create a random number between 0 and the number of lines, and seek to that line. What exactly is the problem?
yes, thats the idea was having. But how do I find how many line break characters there are?
Cheers,
Rob
Cheers,
Rob
Either:
- read the file in some kind of text mode, and read a line at a time until this is no longer possible. Count up the number of lines you read as you did it.
Or:
- read the file as binary, and look at each character in turn, counting the instances of line breaks. For text files under Windows this will be a CR/LF sequence (ascii 13 followed by ascii 10); for anything else it will just be a CR (13). I think I got that right anyway. Damn windows and its bizarre CR/LF legacy >_<
- read the file in some kind of text mode, and read a line at a time until this is no longer possible. Count up the number of lines you read as you did it.
Or:
- read the file as binary, and look at each character in turn, counting the instances of line breaks. For text files under Windows this will be a CR/LF sequence (ascii 13 followed by ascii 10); for anything else it will just be a CR (13). I think I got that right anyway. Damn windows and its bizarre CR/LF legacy >_<
quote:Original post by Anonymous Poster
Open it, read through the entire thing and record the number of line break characters. Then, just create a random number between 0 and the number of lines, and seek to that line. What exactly is the problem?
Actually, the number of lines would be the number of breaks-1, wouldn''t it?
abc
2 breaks, 3 lines.
Depends whether your file ends with a newline. "text files" under Unix-like systems are supposed to, but there isn''t really a way to enforce that given the "everything is a file" mentality. On windows... good luck.
Initialise a counter to zero.
Initialise the chosen line to an empty string.
Attempt to read a line.
If you failed to read a line, you're done; return the chosen line.
Increment counter.
This line has a one in 'counter' chance of being the line you want.
So if a random float from 0.0 to 1.0 is smaller than (1.0 / counter), store the current line as the chosen one.
Repeat from the 'attempt to read a line' part.
[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
[edited by - Kylotan on March 28, 2004 6:57:17 PM]
Initialise the chosen line to an empty string.
Attempt to read a line.
If you failed to read a line, you're done; return the chosen line.
Increment counter.
This line has a one in 'counter' chance of being the line you want.
So if a random float from 0.0 to 1.0 is smaller than (1.0 / counter), store the current line as the chosen one.
Repeat from the 'attempt to read a line' part.
[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
[edited by - Kylotan on March 28, 2004 6:57:17 PM]
I have the same problem as rob
but Im still a noob.,
can anybody provide an example, please.
Thank you!
but Im still a noob.,
can anybody provide an example, please.
Thank you!
If you are using C# you can use System.IO.StreamReader to do the work for you. The ReadLine function will return a line as a string (with a line being defined as ending with a \n or a \r\n combination).
Magius
Magius
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement