开发者

loading binary data for an encryption program

开发者 https://www.devze.com 2023-01-14 21:20 出处:网络
how do i load a file into my program so it\'s just binary. i want to read the binary from a file then save it to another one so the file will be a clone of the first file (if it\'s a exe it will run,

how do i load a file into my program so it's just binary. i want to read the binary from a file then save it to another one so the file will be a clone of the first file (if it's a exe it will run, etc). i would like to store the data in a array or string so i can edit it before i save it. im using windows 7 , mic开发者_开发技巧rosoft c++ 2008.


Something like:

[Edit: added necessary headers: ]

#include <fstream>
#include <algorithm>
#include <vector>
#include <ios>

// define some place to hold the data:
std::vector<char> binary_data;

// open the file and make sure we read it intact:
std::ifstream file("filename.exe", std::ios::binary);
file.unsetf(std::ios_base::skipws);

// read data from file into vector:    
std::copy(std::istream_iterator<char>(file),
          std::istream_iterator<char>(),
          std::back_inserter(binary_data));

// Edit the binary data as needed...

// create new file: 
std::ofstream new_file("new_file.exe", std::ios::binary);

// Write data from vector to new file:
std::copy(binary_data.begin(), 
          binary_data.end(),
          std::ostream_iterator<char>(new_file));

This is pretty elementary C++ though -- my immediate guess would be that you're not really ready to deal with encryption if you don't know this.


The std::ifstream class will do this for you, if you open the file with the ios::binary flag. You will get byte-for-byte the file's contents. If you then write it to another file (ofstream) using ios::binary, you've done a file copy.

If you can use Windows specific API, Windows provides a CopyFile function.

0

精彩评论

暂无评论...
验证码 换一张
取 消