Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #include <random>
  2. #include <iostream>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. float randomFloat() {
  8.     static default_random_engine e{};
  9.     uniform_real_distribution<float> d;
  10.     return d(e);
  11. }
  12.  
  13. int main() {
  14.     int k;
  15.     vector<float> floats;
  16.     std::cin >> k;
  17.     for (int j = 0; j < k; j++) {
  18.         floats.push_back(randomFloat());
  19.     }
  20.  
  21.     ofstream file;
  22.     string filenameText = "pliktxt.txt";
  23.     file.open(filenameText);
  24.     for (float i : floats) {
  25.         file << i;
  26.         file << "\n";
  27.     }
  28.     file.close();
  29.  
  30.     ifstream inputText(filenameText);
  31.     string num;
  32.     if (inputText) {
  33.         cout << "plik tekstowy\n";
  34.         while (getline(inputText, num)) {
  35.             cout << num << "\n";
  36.         }
  37.         inputText.close();
  38.     }
  39.  
  40.     string filenameBin = "plikbin.txt";
  41.     ofstream output(filenameBin, ios::binary);
  42.     if (output) {
  43.         for (int i = 0; i < floats.size(); i++) {
  44.             output.write(reinterpret_cast<char *> (&floats[i]), sizeof(i));
  45.         }
  46.     }
  47.     output.close();
  48.  
  49.     ifstream inputBin(filenameBin, ios::binary);
  50.     if(inputBin) {
  51.         float i;
  52.         cout << "plik binarny\n";
  53.         while(inputBin.read(reinterpret_cast<char*> (&i), sizeof(i))) {
  54.             cout << i << " ";
  55.         }
  56.     }
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement