fork download
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. union dbl {
  5. char mem[8];
  6. double d;
  7. };
  8.  
  9. int main()
  10. {
  11. double d[] = {1.0, 2.1, 3.2, 4.3};
  12.  
  13. std::ofstream fout("dataset.dat", std::ios::binary);
  14. for (double* dp = d; dp != &d[sizeof(d) / sizeof(d[0])]; dp++)
  15. fout.write(reinterpret_cast<char*>(dp), static_cast<std::streamsize>(sizeof(double)));
  16. fout.close();
  17.  
  18. std::ifstream fin("dataset.dat", std::ios::binary);
  19. std::istreambuf_iterator<char> buf(fin), eof;
  20.  
  21. for (; buf != eof;) {
  22. dbl c;
  23. c.mem[0] = *buf++;
  24. c.mem[1] = *buf++;
  25. c.mem[2] = *buf++;
  26. c.mem[3] = *buf++;
  27. c.mem[4] = *buf++;
  28. c.mem[5] = *buf++;
  29. c.mem[6] = *buf++;
  30. c.mem[7] = *buf++;
  31. std::cout << c.d << std::endl;
  32. }
  33. }
  34.  
Success #stdin #stdout 0.01s 2856KB
stdin
Standard input is empty
stdout
Standard output is empty