シリアル ポート コードを書いていて、ファイルの内容 (バイナリ) を変数に読み込む必要があります。http://www.cplusplus.com/doc/tutorial/files/の「バイナリ ファイル」の例から始めて、.jpg ファイルを開こうとします。
#include <iostream>
#include <fstream>
using namespace std;
ifstream::pos_type size;
char * memblock;
int main () {
ifstream file ("example.jpg", ios::in|ios::binary|ios::ate);
if (file.is_open())
{
size = file.tellg();
memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();
cout << memblock << endl;
delete[] memblock;
}
else cout << "Unable to open file";
return 0;
}
ただし、コンソールに表示されるのは最初の 4 文字 (32 ビット) だけです。
ただし、特に奇妙なのは、問題があると思われる変数「memblock」で ostream::write() を使用すると、完全に機能することです。
ofstream fileOut ("writtenFile.jpg",ios::out|ios::binary);
fileOut.write(memblock,size);
fileOut.close();
つまり、新しい .jpg ファイルを作成します。
私の質問は、memblock 変数に最初の 4 文字しか含まれていないように見える理由です。