Thread: Random line from txt file

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    19

    Random line from txt file

    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-

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    19
    I appology for my stupidity, but how can I read that to array?
    Last edited by dogbert234; 05-31-2005 at 02:30 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    or maybe fgets would do the trick

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    another (possibly less efficient) method:

    you could pick a random number and ignore that many lines... for example:
    Code:
    for(i=0;i<randomNumber;i++)  //loop through until you reach a certain line
    {
        infile.ignore(32000,'\n');  //ignore a line
    }
    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.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help again with scrolling without wrapping
    By Dukefrukem in forum C Programming
    Replies: 8
    Last Post: 09-21-2007, 12:48 PM
  2. Retrieve string(s) from (txt) file
    By Gordon in forum Windows Programming
    Replies: 12
    Last Post: 08-10-2007, 03:34 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM