tags:

views:

39

answers:

2

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 , microsoft c++ 2008.

+2  A: 

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.

Jerry Coffin
what are the #includes i need for this? i get 21 errors
blood
@blood: I've added the necessary headers (at least I *think* I caught all that are needed...)
Jerry Coffin
hmm ok ty for the headers i still have 3 errors. "error C2065: 'binary_file' : undeclared identifier" -- "error C2228: left of '.unsetf' must have class/struct/union 1> type is ''unknown-type''" -- "error C2440: '<function-style-cast>' : cannot convert from 'std::ifstream' to 'std::ostream_iterator<_Ty>' 1> with 1> [ 1> _Ty=char 1> ] 1> No constructor could take the source type, or constructor overload resolution was ambiguous" any ideas? also i g2g for now so if you can fix the errors tyvm if not i'll try later ty
blood
Probably copy and paste the current code -- I had a typo where `ifstream` was mis-typed as 'isftream` (or something like that). The errors you're seeing sound reasonable for the code with that typo.
Jerry Coffin
@Jerry: If he's just copy pasting things, it probably won't compile because it's not in a function. +1.
Billy ONeal
@billy Oneal ... i put it in a program. @Jerry coffin no i think i got that typo because i can't find it and i fixed some of the errors before i posted but i had to go so did not get to work on more work on it. but ty :D
blood
ok i got it working :D it seems to work right so tyvm ^^
blood
A: 

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.

Thanatos
Copying the file won't be all that useful if he want's to encrypt it on the way :)
Billy ONeal
@billy Oneal - lol :D that's what i was going to say
blood