Reading in a random line from a file

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2007
Posts: 110
Reputation: kylcrow is an unknown quantity at this point 
Solved Threads: 2
kylcrow kylcrow is offline Offline
Junior Poster

Reading in a random line from a file

 
0
  #1
Apr 6th, 2007
Hi. I am new to the forum and I had a question. I have looked around and haven't really been able to find exactly what I am looking for.

I am looking for the code in C++ to read in a random single line with spaces from a file.

This is the file I have...(example.txt)

the big green
forrest gump
blades of glory
glory road
animal house
pirates of the carribean
the seven little foys
the lord of the rings
the green mile
cast away
star wars
it
now and then
the princess bride



This is the code for a function I have so far, but it just prints out the entire file.

  1.  
  2. void file(){
  3. string line;
  4. ifstream myfile ("example.txt");
  5. if (myfile.is_open())
  6. {
  7. while (! myfile.eof())
  8. {
  9. getline (myfile,line);
  10. cout << line << endl;
  11. }
  12. myfile.close();
  13. }
  14. else cout << "Unable to open file";
  15. }

how do i change this so that it reads in just a random single line?
Last edited by kylcrow; Apr 6th, 2007 at 2:22 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,374
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Reading in a random line from a file

 
0
  #2
Apr 6th, 2007
just count the lines as they are read and stop when it has read the line you need. Toss all lines but the last one away.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Reading in a random line from a file

 
0
  #3
Apr 6th, 2007
1. Read in each line into a string array or std::vector.

2. Whilst at the same time incrementing a counter, which counts each line.

3. Using srand pick a number which is less than the max line count.

4. Use that number as the index to the array of strings/ vector and output to the screen.

Alternatively you can do something similar without the need for a temporary storage device (vector etc).
Last edited by iamthwee; Apr 6th, 2007 at 2:25 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 110
Reputation: kylcrow is an unknown quantity at this point 
Solved Threads: 2
kylcrow kylcrow is offline Offline
Junior Poster

Re: Reading in a random line from a file

 
0
  #4
Apr 6th, 2007
I think I understand, but I am new to C++ so could you show me an example of the actual code? It was be greatly appreciated.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Reading in a random line from a file

 
0
  #5
Apr 6th, 2007
Well the first thing you need to do is figure out how to generate random numbers.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Reading in a random line from a file

 
0
  #6
Apr 6th, 2007
I don't have my compiler so this is not tested.

  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <vector>
  5. #include <string>
  6. #include <ctime>
  7.  
  8. class RandomCrap
  9. {
  10. public:
  11. int show(int p)
  12. {
  13. srand( ( unsigned )time( 0 ) );
  14. int random_integer;
  15.  
  16. random_integer = ( rand ( ) % p ) + 1;
  17. return random_integer;
  18. }
  19. };
  20.  
  21. int main()
  22. {
  23. std::ifstream read ("example.txt");
  24.  
  25. std::string line;
  26. std::vector < std::string > stuff;
  27.  
  28. int counter = 0;
  29. while (getline(read, line, '\n'))
  30. {
  31. stuff.push_back( line );
  32. counter++;
  33. }
  34.  
  35.  
  36. read.close();
  37.  
  38. RandomCrap a;
  39. std::cout << stuff[a.show( counter - 1 )];
  40.  
  41. std::cin.get();
  42. return 0;
  43. }

I think it might always miss the first line. Meh?
Last edited by iamthwee; Apr 6th, 2007 at 3:30 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Reading in a random line from a file

 
0
  #7
Apr 6th, 2007
the basic problem here is that we need to select one item at random
when we do not know beforehand how many items there are. the most
common example of this in real code is when a random element has to
be picked from a list (size unknown). there is a well known algorithm to
do this without having to pass through the entire sequence:
  1. string random_line( ifstream file )
  2. {
  3. string line ;
  4. int nlines = 0 ;
  5. while( getline( file, line ) )
  6. if( ( rand() % ++nlines ) == 0 ) break ;
  7. return line ;
  8. }
the variable nlines counts the number of lines as they are read.
for any line, rand() % ++nlines == 0 is true with probability
1 / nlines. the first line is initially selected (with probability 1),
the second line replaces it with probability 1/2, the third line will
replace the survivor (either line one or line 2, each of which are
equally likely) with probability 1/3, and so on.therefore, for each
of the nlines lines seen so far is selected with probability
1/nlines.

if i remember right, this was first seen widely in some code
written by brian kernighan.
Last edited by vijayan121; Apr 6th, 2007 at 8:55 pm. Reason: add the last 2 lines
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Reading in a random line from a file

 
0
  #8
Apr 7th, 2007
THERE IS A SERIOUS ERROR IN THE PREVIOUS POST.
there is a well known algorithm to
do this without having to pass through the entire sequence:
  1. string random_l[ine( ifstream file )
  2. {
  3. string line ;
  4. int nlines = 0 ;
  5. while( getline( file, line ) )
  6. if( ( rand() % ++nlines ) == 0 ) break ;
  7. return line ;
  8. }
IT SHOULD BE
there is a well known algorithm to
do this without having to copy the entire sequence
into a buffer having random access :
  1. string random_line( ifstream file )
  2. {
  3. string line ;
  4. string selected_line ; // added
  5. int nlines = 0 ;
  6. while( getline( file, line ) )
  7. if( ( rand() % ++nlines ) == 0 ) // break ;
  8. selected_line = line ;
  9. return selected_line ;
  10. }
Last edited by vijayan121; Apr 7th, 2007 at 2:11 am. Reason: add code tag
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC