Pella86

Lettura files ifstream

Apr 26th, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include <iostream>
  2. #include "Node.h"
  3.  
  4. #include <fstream>
  5.  
  6. void write_binary()
  7. {
  8.     std::ofstream bin("tst.bi", std::ios::out|std::ios::binary);
  9.     double v = 7;
  10.     bin << v;
  11.     bin.close();
  12. }
  13.  
  14. void read_binary()
  15. {
  16.     std::ifstream bin("tst.bi", std::ios::in|std::ios::binary);
  17.     double v;
  18.     bin >> v;
  19.     std::cout << v << std::endl;
  20. }
  21.  
  22.  
  23. std::ofstream& operator << (std::ofstream& out, const double& v)
  24. {
  25.     out << v;
  26.     return out;
  27. }
  28.  
  29. std::ifstream& operator >> (std::ifstream& in, double& v)
  30. {
  31.     in >> v;
  32.     return in;
  33. }
  34.  
  35. int main()
  36. {
  37.     //test_node();
  38.  
  39.     write_binary();
  40.     read_binary();
  41.  
  42.     std::ofstream ofile("test.bi", std::ios::out|std::ios::binary);
  43.     double v = 7;
  44.     ofile << v;
  45.  
  46.     std::ifstream ifile("test.bi", std::ios::in|std::ios::binary);
  47.     double iv;
  48.     ifile >> iv;
  49.     std::cout << iv << std::endl;
  50.  
  51.     std::cout << "Hello world!" << std::endl;
  52.     return 0;
  53. }
Add Comment
Please, Sign In to add comment