• Create Account

## Random line in a text file. [C#]

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1rob64464  Members

Posted 26 March 2004 - 08:28 AM

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]

### #2rob64464  Members

Posted 26 March 2004 - 08:29 AM

### #3Anonymous Poster_Anonymous Poster_*  Guests

Posted 26 March 2004 - 08:43 AM

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?

### #4rob64464  Members

Posted 26 March 2004 - 09:05 AM

yes, thats the idea was having. But how do I find how many line break characters there are?

Cheers,
Rob

### #5Zahlman  Members

Posted 26 March 2004 - 09:45 AM

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 >_<

### #6yaroslavd  Members

Posted 27 March 2004 - 12:36 PM

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.

### #7Zahlman  Members

Posted 27 March 2004 - 09:29 PM

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.

### #8Kylotan  Moderators

Posted 28 March 2004 - 11:56 AM

Initialise a counter to zero.
Initialise the chosen line to an empty string.
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]

### #9Anonymous Poster_Anonymous Poster_*  Guests

Posted 04 May 2004 - 11:40 PM

I have the same problem as rob
but Im still a noob.,
can anybody provide an example, please.

Thank you!

### #10Magius  Members

Posted 05 May 2004 - 12:03 AM

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

### #11EbonySeraphim  Members

Posted 05 May 2004 - 05:04 AM

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.

### #12Arild Fines  Members

Posted 05 May 2004 - 05:54 AM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.