Advertisement
Guest User

Untitled

a guest
Aug 5th, 2015
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. std::string targetFile = "simplehashingfile.txt";
  2. const char* filename = targetFile.c_str();
  3. std::ifstream file;
  4.  
  5. file.open( filename, std::ios::binary | std::ios::in );
  6. file.seekg(0, std::ios::end); // go to end of file
  7. std::streamsize size = file.tellg(); // get size of file
  8.  
  9. std::vector<char> buffer(size); // create vector of file size bytes
  10.  
  11. file.read(buffer.data(), size); // read file into buffer vector
  12. int totalread = file.gcount();
  13.  
  14. // Check that data was read
  15. std::cout<<"total read: " << totalread << std::endl;
  16.  
  17.  
  18. // check buffer:
  19. std::cout<<"from buffer vector: "<<std::endl;
  20. for (int i=0; i<size; i++){
  21. std::cout << buffer[i] << std::endl;
  22. }
  23. std::cout<<"nn";
  24.  
  25. // Writing binary to file
  26. std::ofstream ofile;
  27. ofile.open("testbinary", std::ios::out | std::ios::binary);
  28.  
  29. uint32_t bytes4 = 0x7FFFFFFF; // max 32-bit value
  30. uint32_t bytes8 = 0x12345678; // some 32-bit value
  31.  
  32.  
  33. ofile.write( (char*)&bytes4 , 4 );
  34. ofile.write( (char*)&bytes8, 4 );
  35.  
  36. ofile.close();
  37.  
  38.  
  39.  
  40. // Reading from file
  41. std::ifstream ifile;
  42. ifile.open("testbinary", std::ios::out | std::ios::binary);
  43.  
  44. uint32_t reading; // variable to read data
  45. uint32_t reading2;
  46.  
  47. ifile.read( (char*)&reading, 4 );
  48. ifile.read( (char*)&reading2, 4 );
  49.  
  50. std::cout << "The file contains: " << std::hex << reading << std::endl;
  51. std::cout<<"next 4 bytes: "<< std::hex << reading2 << std::endl;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement