Random line from txt file

This is a discussion on Random line from txt file within the C++ Programming forums, part of the General Programming Boards category; Hi, I hope this question isn't to stupid, but how can I get a random line from txt file? Google ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,656
    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 support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,656
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    #include<xErath.h> xErath's Avatar
    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, 01:53 AM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21