Advertisement
paulogp

Ficheiro de texto

Aug 21st, 2011
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. // apple xcode
  2. // paulogp
  3. #include <iostream>
  4. #include <fstream>
  5. #include <string>
  6.  
  7. #include <cstdlib>
  8. #include <ctime>
  9. #include <fstream>
  10.  
  11. using namespace std;
  12.  
  13. int main (int argc, const char * argv[])
  14. {
  15.     // input
  16.     ifstream the_input_stream ("infile.txt");
  17.     string the_first, the_last;
  18.    
  19.     if (the_input_stream.is_open())
  20.     {
  21.         while (the_input_stream)
  22.         {
  23.             the_input_stream >> the_first >> the_last;
  24.             cout << the_last << ", " << the_first << endl;
  25.         }
  26.     }
  27.    
  28.     the_input_stream.close();
  29.    
  30.     // output
  31.     int the_rand;
  32.    
  33.     srand ((unsigned)time (0)); // positive rand
  34.    
  35.     ofstream the_output_stream ("outputfile.txt", ios::trunc); // ios::trunc to NOT append file
  36.    
  37.     for (int i = 0; i < 1000000; i++)
  38.     {
  39.         the_rand = (rand() % 100) + 1; // random [1, 100]
  40.         the_output_stream << the_rand << endl;
  41.     }
  42.    
  43.     the_output_stream.close();
  44.    
  45.     // how may even number are inside the file
  46.     int the_temp;
  47.     int the_even = 0;
  48.    
  49.     the_input_stream.open ("outputfile.txt");
  50.    
  51.     while (the_input_stream)
  52.     {
  53.         the_input_stream >> the_temp;
  54.         if ((the_temp % 2) == 0)
  55.         {
  56.             the_even ++;
  57.         }
  58.     }
  59.    
  60.     the_input_stream.close();
  61.    
  62.     cout << "pares: " << the_even << endl;
  63.     cout << "done" << endl;
  64.    
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement