avr39ripe

cppBinaryFileComparisionWithText

Jun 22nd, 2021
735
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. int main()
  5. {
  6.     std::ofstream outf;
  7.     std::ofstream outfT;
  8.     outfT.open("textFile.txt");
  9.     outf.open("binaryFile.dat", std::ios::binary);
  10.  
  11.     int outVal{ 255 };
  12.     for (int i{ 1 }; i < 10; ++i)
  13.     {
  14.         outfT << outVal << '\n';
  15.         outf.write((char*)(&outVal), sizeof(int));
  16.         ++outVal;
  17.     }
  18.  
  19.     outf.close();
  20.     outfT.close();
  21.  
  22.     std::ifstream inf;
  23.  
  24.     inf.open("binaryFile.dat", std::ios::binary);
  25.  
  26.     int val{ 1 };
  27.  
  28.     for (int i{ 1 }; i < 10; ++i)
  29.     {
  30.         inf.read((char*)(&val), sizeof(int));
  31.         std::cout << val << '\n';
  32.     }
  33.  
  34.     inf.close();
  35. };
  36.  
RAW Paste Data