On this instance we are going to take a look at this method utilizing the SDL library.
Let’s change the patch.
If utilizing solely 2 arguments it should get the dimensions of the file itself
patch primary.exe knowledge.txt
If we use 3 arguments we are able to return to the unique file dimension
patch primary.exe knowledge.txt 246784
1) Let’s replace the patch:
#embody <fstream>
#embody <iostream>
#embody <string>
#embody <vector>
#embody <sstream>
std::vector<char> file_read_bin(const std::string& fileName) {
std::string filePath = fileName;
std::ifstream file(filePath, std::ios::binary);
if (file.fail()) {
std::cerr << "Open file error " << filePath << std::endl;
}
// Learn bytes
std::vector<char> bytFile((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
file.shut();
return bytFile;
}
void file_write_bin(const std::string& filePath, std::vector<char> bytFile) {
// Write bytes
std::ofstream outFile(filePath, std::ios::binary);
if (outFile.fail()) {
std::cerr << "Open file error " << filePath << " to put in writing" << std::endl;
}
outFile.write(bytFile.knowledge(), bytFile.dimension());
outFile.shut();
std::cout << "File sucessfull up to date" << std::endl;
}
int primary(int argc, char *argv[]){
int correctSize = 0;
if (argc < 2) {
std::cout << "Require 2 or 3 args: program.exe knowledge.txt 163857n";
return 1;
}
const char* program_file = argv[1];
const char* data_file = argv[2];
std::vector<char> bytFile = file_read_bin( program_file );
std::vector<char> bytText = file_read_bin( data_file );
int dimension = bytFile.dimension();
if(argc == 3) {
correctSize = dimension;
}else if(argc == 4) {
correctSize = std::stoi(argv[3]);
}
int diff = dimension - correctSize;
// Logs
std::cout << "dimension: " << dimension << "n";
std::cout << "correctSize: " << correctSize << "n";
// Take away previous code
if(dimension > correctSize ){
bytFile.erase(bytFile.finish() - diff, bytFile.finish() );
}
// Add content material
bytFile.insert(bytFile.finish(), bytText.start(), bytText.finish());
// Add content material dimension
bytFile.insert(bytFile.finish(), {
static_cast<char>((bytText.dimension() >> 24) & 0xFF),
static_cast<char>((bytText.dimension() >> 16) & 0xFF),
static_cast<char>((bytText.dimension() >> 8) & 0xFF),
static_cast<char>((bytText.dimension() >> 0) & 0xFF)
});
// Add file title
std::string data_filename_str(data_file);
data_filename_str+="