Advertisement
madras

Untitled

May 19th, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. int main()
  5. {
  6. // zapis pieciu liczb double 3.14, 4.14, 5.14 ...
  7.     try
  8.     {
  9.         std::ofstream file("out.txt", std::ios::binary);
  10.         file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
  11.  
  12.         for(int i = 0; i < 5; i++)
  13.         {
  14.             double pi = 3.14;
  15.             pi += i;
  16.             file.write( reinterpret_cast<char*>( &pi ), sizeof(pi));
  17.         }
  18.     }
  19.     catch(std::ios_base::failure &fail)
  20.     {
  21.         std::cout << "Nie mozna zapisac pliku!" << std::endl
  22.             << fail.what() << std::endl;
  23.     }
  24.  
  25.     double x;
  26.  
  27. // odczyt tego samego pliku
  28.     try
  29.     {
  30.         std::ifstream in("out.txt");
  31.         in.exceptions(std::ifstream::failbit | std::ifstream::badbit);
  32.         while(!in.eof())
  33.         {
  34.             in.read(reinterpret_cast<char*>(&x), sizeof(x));
  35.             std::cout << x << std::endl;
  36.         }
  37.     }
  38.     catch(std::ios_base::failure &fail)
  39.     {
  40.         // handle exception here
  41.             std::cout << "Caught an exception: " << fail.what() << std::endl;
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement