Archived

This topic is now archived and is closed to further replies.

Random line in a text file. [C#]

This topic is 4968 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Guest 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?

Share this post


Link to post
Share on other sites
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 >_<

Share this post


Link to post
Share on other sites
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?


a
b
c


2 breaks, 3 lines.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I have the same problem as rob
but Im still a noob.,
can anybody provide an example, please.

Thank you!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
A better, and possibly slower, way might be if you read in a byte(character by character) at a time, and basically check for newline characters and count them up. I think that is the easiest way because some of the other methods may eliminate whitespaces for you and break at parts other than a new line.

Share this post


Link to post
Share on other sites
quote:
Original post by EbonySeraphim
A better, and possibly slower, way might be if you read in a byte(character by character) at a time, and basically check for newline characters and count them up. I think that is the easiest way because some of the other methods may eliminate whitespaces for you and break at parts other than a new line.

Nope, ReadLine is the superior way of doing this. Your suggestion is a hack.

Share this post


Link to post
Share on other sites