2010-09-03 50 views
0

如何將文件加載到我的程序中,所以它只是二進制。我想從文件中讀取二進制文件,然後將其保存到另一個文件中,以便該文件成爲第一個文件的克隆(如果它是一個exe文件,它將運行,等等)。我想將數據存儲在數組或字符串中,以便在保存之前對其進行編輯。使用Windows 7 IM,微軟Visual C++ 2008加載加密程序的二進制數據

+3

不知道這個問題的答案表明,你需要一個好的[圖書](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)。 – 2010-09-03 04:15:14

+0

@Billy ONeal好書不錯,但我並不真的需要一本完整的書,我真的很想知道如何閱讀二進制文件。 – blood 2010-09-03 04:32:05

+0

如果明天你必須讀取TEXT中的2個文件並將其保存爲4個文件,該怎麼辦? – Chubsdad 2010-09-03 04:45:40

回答

2

喜歡的東西:

[編輯:添加必要的標頭:]

#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)); 

這是很基本的C++,但 - 我的頂頭的猜測是,你如果你不知道這一點,那麼你還沒有準備好處理加密。

+0

我需要什麼#包括這個?我得到21個錯誤 – blood 2010-09-03 04:27:27

+0

@blood:我已經添加了必要的標頭(至少我認爲* *我抓住了所需的所有...) – 2010-09-03 04:41:16

+0

嗯OK TY的頭,我仍然有3個錯誤。 「error C2065:'binary_file':未聲明的標識符」 - 「錯誤C2228:'.unsetf'的左邊必須有class/struct/union 1> type爲''unknown-type''」 - 「error C2440:'<函數式鑄>」:不能從轉換 '的std :: ifstream的<_Ty> '1>' 到' 的std :: ostream_iterator其中1> [1> _Ty = CHAR 1> 1>無構造可以採取源類型,或構造函數重載解析模糊「任何想法?此外,我G2G現在如果你能修正錯誤tyvm如果沒有,我會稍後再試TY – blood 2010-09-03 04:49:21

0

如果您使用ios::binary標誌打開文件,std::ifstream類將爲您執行此操作。你會得到字節爲字節的文件的內容。如果您使用ios::binary將它寫入另一個文件(ofstream),則您已完成文件複製。

如果你可以使用Windows特定的API,Windows提供了一個CopyFile功能。

+0

複製文件,如果他想的到它的加密方式也不會那麼有用:) – 2010-09-03 13:28:19

+0

@billy - 奧尼爾笑: D這就是我要說的 – blood 2010-09-03 21:04:37