Hi,
I hope this question isn't to stupid, but how can I get a random line from txt file? Google didn't help me much at that point, so maybe you could
Best regards,
dogbert234-
Hi,
I hope this question isn't to stupid, but how can I get a random line from txt file? Google didn't help me much at that point, so maybe you could
Best regards,
dogbert234-
Read them all into an array of lines
Count how many you read (=n)
Pick one of them (array[rand()%n)
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I appology for my stupidity, but how can I read that to array?
Last edited by dogbert234; 05-31-2005 at 02:30 AM.
Well a std::vector of std::string would seem to be the way to go.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
or maybe fgets would do the trick
another (possibly less efficient) method:
you could pick a random number and ignore that many lines... for example:of course (or maybe not) this takes two reads of the file (which would slow it immensely) or prior knowledge of how many lines there are in it.Code:for(i=0;i<randomNumber;i++) //loop through until you reach a certain line { infile.ignore(32000,'\n'); //ignore a line }
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT