Advertisement
Bartek16194

Losowanie z pliku

Nov 21st, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.53 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <time.h>
  6. #include <stdlib.h>
  7. using namespace std;
  8.  
  9. int main ()
  10. {
  11.     int number_of_lines = 0;
  12.     string line;
  13.     ifstream myfile("abc.txt");
  14.  
  15.     if(!myfile.is_open()) return 0;
  16.     while(getline(myfile, line)) number_of_lines++;
  17.     srand(time(NULL));
  18.     int rand_val = rand() % number_of_lines + 1;
  19.     myfile.clear();
  20.     myfile.seekg (0, myfile.beg);
  21.     for(int i = 0; i < rand_val; i++) getline(myfile, line);
  22.     cout << line << endl;
  23.     return 0;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement