thecplusplusguy

get a random line example

Aug 29th, 2011
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. //http://www.youtube.com/user/thecplusplusguy
  2. #include <iostream> //for write out (cout)
  3. #include <fstream>  //for file handling (ifstream)
  4. #include <vector>       //for store the lines   (vector)
  5. #include <string>       //for store the strings
  6. #include <ctime>        //for seed the random number generator (time(0))
  7. #include <cstdlib>  //for random number generator (rand)
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12.     char buf[500]; //max 500 character/line
  13.     ifstream in("example.txt"); //read the example.txt file
  14.     vector<string> lines;   //create a vector, which contain strings
  15.     srand(time(NULL));  //seed the random number generator
  16.     //read all lines to the vector
  17.     while(!in.eof())    //while we are not the end of the file
  18.     {
  19.         in.getline(buf,500);    //read a line to the buf (max 500 character, from in, to buf)
  20.         lines.push_back(buf);   //push to the end of the lines vector
  21.     }
  22.     in.close(); //close the file, we don't need it anymore
  23.     for(int i=0;i<10;i++)   //get 10 random number
  24.     {
  25.         int randline=rand()%(lines.size()-1);   //generate a random number, I use the -1 for the size, because it starts from 1 (if the vector 20 long, the max
  26.         //number you get randomly is 20, but the biggest index is 19)
  27.         cout << lines[randline] << endl;    //write out the line
  28.     }
  29.     return 0;
  30. }
  31.  
  32. /*
  33. solution 2, without read it to the memory
  34. #include <iostream> //for write out (cout)
  35. #include <fstream>  //for file handling (ifstream)
  36. #include <ctime>        //for seed the random number generator (time(0))
  37. #include <cstdlib>  //for random number generator (rand)
  38. using namespace std;
  39.  
  40. int main()
  41. {
  42.     char buf[500]; //max 500 character/line
  43.     ifstream in("example.txt"); //read the example.txt file
  44.     int lines=0;
  45.     srand(time(NULL));  //seed the random number generator
  46.     //read all lines to the vector
  47.     while(!in.eof())    //while we are not the end of the file
  48.     {
  49.         in.getline(buf,500);    //read a line to the buf (max 500 character, from in, to buf)
  50.         lines++;
  51.     }
  52.     for(int i=0;i<10;i++)   //get 10 random number
  53.     {
  54.         in.clear();
  55.         in.seekg(0,ios::beg);
  56.  
  57.         int randline=rand()%(lines-1)+1;    //generate a random number, I use the -1 for the size, because it starts from 1 (if the vector 20 long, the max
  58.         //number you get randomly is 20, but the biggest index is 19)
  59.         for(int i=0;i<randline;i++)
  60.         {
  61.             in.getline(buf,500);
  62.  
  63.         }
  64.         cout << buf << endl;
  65.     }
  66.     in.close(); //close the file, we don't need it anymore
  67.     return 0;
  68. } */
Advertisement
Add Comment
Please, Sign In to add comment