0

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.

blood
  • 746
  • 5
  • 13
  • 22
  • 3
    Not knowing the answer to this question suggests that you need a good [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Billy ONeal Sep 03 '10 at 04:15
  • @Billy ONeal a good book would be nice but i don't really need a full book i just really wanted to know how to read the binary. – blood Sep 03 '10 at 04:32
  • what if tomorrow you have to read 2 files in TEXT and save it to 4 files? – Chubsdad Sep 03 '10 at 04:45
  • 1
    They mean you're asking a question which implies you don't know the very fundamental basics of c++, and that's something that even if we give you the answer, you'll need to know. Anyway, here's a like for your specific question, start from there: http://www.cplusplus.com/reference/clibrary/cstdio/fopen/ – Poni Sep 03 '10 at 04:55
  • @chubsdad .... what are you talking about. – blood Sep 03 '10 at 21:11

2 Answers2

2

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
  • 476,176
  • 80
  • 629
  • 1,111
  • @blood: I've added the necessary headers (at least I *think* I caught all that are needed...) – Jerry Coffin Sep 03 '10 at 04:41
  • 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: '' : 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 Sep 03 '10 at 04:49
  • 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 Sep 03 '10 at 05:44
  • 1
    @Jerry: If he's just copy pasting things, it probably won't compile because it's not in a function. +1. – Billy ONeal Sep 03 '10 at 13:27
  • @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 Sep 03 '10 at 21:03
0

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
  • 42,585
  • 14
  • 91
  • 146