Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


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.

  • You cannot reply to this topic
11 replies to this topic

#1 rob64464   Members   -  Reputation: 100

Like
Likes
Like

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]

Sponsor:

#2 rob64464   Members   -  Reputation: 100

Like
Likes
Like

Posted 26 March 2004 - 08:29 AM

sorry about spelling mistake

#3 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

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?

#4 rob64464   Members   -  Reputation: 100

Like
Likes
Like

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

#5 Zahlman   Moderators   -  Reputation: 1682

Like
Likes
Like

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

#6 yaroslavd   Members   -  Reputation: 150

Like
Likes
Like

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?


a
b
c


2 breaks, 3 lines.

#7 Zahlman   Moderators   -  Reputation: 1682

Like
Likes
Like

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.

#8 Kylotan   Moderators   -  Reputation: 3338

Like
Likes
Like

Posted 28 March 2004 - 11:56 AM

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]

#9 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

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!


#10 Magius   Members   -  Reputation: 220

Like
Likes
Like

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

#11 EbonySeraphim   Members   -  Reputation: 131

Like
Likes
Like

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.

#12 Arild Fines   Members   -  Reputation: 968

Like
Likes
Like

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.



PARTNERS