Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::string targetFile = "simplehashingfile.txt";
- const char* filename = targetFile.c_str();
- std::ifstream file;
- file.open( filename, std::ios::binary | std::ios::in );
- file.seekg(0, std::ios::end); // go to end of file
- std::streamsize size = file.tellg(); // get size of file
- std::vector<char> buffer(size); // create vector of file size bytes
- file.read(buffer.data(), size); // read file into buffer vector
- int totalread = file.gcount();
- // Check that data was read
- std::cout<<"total read: " << totalread << std::endl;
- // check buffer:
- std::cout<<"from buffer vector: "<<std::endl;
- for (int i=0; i<size; i++){
- std::cout << buffer[i] << std::endl;
- }
- std::cout<<"nn";
- // Writing binary to file
- std::ofstream ofile;
- ofile.open("testbinary", std::ios::out | std::ios::binary);
- uint32_t bytes4 = 0x7FFFFFFF; // max 32-bit value
- uint32_t bytes8 = 0x12345678; // some 32-bit value
- ofile.write( (char*)&bytes4 , 4 );
- ofile.write( (char*)&bytes8, 4 );
- ofile.close();
- // Reading from file
- std::ifstream ifile;
- ifile.open("testbinary", std::ios::out | std::ios::binary);
- uint32_t reading; // variable to read data
- uint32_t reading2;
- ifile.read( (char*)&reading, 4 );
- ifile.read( (char*)&reading2, 4 );
- std::cout << "The file contains: " << std::hex << reading << std::endl;
- std::cout<<"next 4 bytes: "<< std::hex << reading2 << std::endl;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement