944,067 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 10697
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 6th, 2007
0

Reading in a random line from a file

Expand Post »
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.

c++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 11
Solved Threads: 2
Junior Poster
kylcrow is offline Offline
124 posts
since Apr 2007
Apr 6th, 2007
0

Re: Reading in a random line from a file

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Apr 6th, 2007
0

Re: Reading in a random line from a file

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 6th, 2007
0

Re: Reading in a random line from a file

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.
Reputation Points: 11
Solved Threads: 2
Junior Poster
kylcrow is offline Offline
124 posts
since Apr 2007
Apr 6th, 2007
0

Re: Reading in a random line from a file

Well the first thing you need to do is figure out how to generate random numbers.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 6th, 2007
0

Re: Reading in a random line from a file

I don't have my compiler so this is not tested.

C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 6th, 2007
0

Re: Reading in a random line from a file

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:
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Apr 7th, 2007
0

Re: Reading in a random line from a file

THERE IS A SERIOUS ERROR IN THE PREVIOUS POST.
Quote ...
there is a well known algorithm to
do this without having to pass through the entire sequence:
C++ Syntax (Toggle Plain Text)
  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 :
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Mar 13th, 2011
0

Problems with the method

Both of the previous algorithms are seriously flawed. They never choose the first line, while the second line has a 50% probability of being picked up. For any subsequent line, the probability of being picked up is decreasing.

Another idea would be to jump in the file (fseek) at a random offset and read the current line. However, this would favorize the large lines. We can pick a line pointed by the random offset with the probability of 1/string_length. This means, if the average length of a line is 100, we will read on average about 100 lines until picking one up. Even if the procedure is statistically correct, it is inefficient, as it reads much more lines than needed.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
razvan1 is offline Offline
14 posts
since Mar 2011
Mar 14th, 2011
0
Re: Reading in a random line from a file
Nice info, but 4 years too late.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: c++ formulas
Next Thread in C++ Forum Timeline: Help with temporary files in temp folder





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC