私は C++ が初めてで、学校の課題をやらなければなりません。
API 呼び出しやシステム統合コマンドを使用せずにバイナリ* ファイルをコピーする必要があります。学校では Windows マシンを使用しています。
少し調べてみたところ、API を使用せずにデータをコピーする最善の方法は、iostream (ifstream/fstream) を使用することであることがわかりました。使用しているコードは次のとおりです。
int Open(string Name){
int length;
char * buffer;
ifstream is;
fstream out;
FILE* pFile;
is.open (Name.c_str(), ios::binary );
// get length of file:
is.seekg (0, ios::end);
length = is.tellg();
is.seekg (0, ios::beg);
// allocate memory:
buffer = new char [length];
// read data as a block:
is.read (buffer,length);
is.close();
pFile = fopen ( "out.exe" , "w" );
fclose(pFile);
out.open("out.exe", ios::binary);
out.write( buffer, length);
out.close();
delete[] buffer;
return 0;
}
out.exe が正常に動作していません。winhex.exe で調べたところ、何もしていないのにデータが変更されていることがわかりました。
誰でも私を助けることができますか?
*このファイルは単純な hello world プログラムで、メッセージ ボックスに "hello world" が含まれています。
編集:
無反応でごめんなさい、寝てました。とにかく、16進エディタ内で(結果と元の)両方のプログラムを開きました。私はすべてこの行を試しているようです:
Offset 0 1 2 3 4 5 6 7 8 9 A B C D E F
00000200 4C 00 00 00 00 30 00 00 00 02 00 00 00 0D 0A 00 L 0
これに変更:
Offset 0 1 2 3 4 5 6 7 8 9 A B C D E F
00000200 4C 00 00 00 00 30 00 00 00 02 00 00 00 0A 00 00 L 0
読み取りまたは書き込みプロセス中に何らかの形で表示または表示できないように、バイトが削除されます (または追加されることもあります)。