o.write
文字を書き込まず、バイトを書き込みます (ios::binary でフラグが立てられている場合)。char の長さは 1 バイトであるため、char-pointer が使用されます。
o.write((char*)a,sizeof(a));
(char*) a
o.write
書き込み先のアドレスです。sizeof(a)
次に、バイトをファイルに書き込みます。文字は格納されず、バイトのみが格納されます。
int i = 10
Hex-Editor でファイルを開くと、a が:
0A 00 00 00
(4 バイト、x64)の場合、次のように表示されます。
読み方はアナログ。
これが実際の例です:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main (int argc, char* argv[]){
const char* FILENAM = "a.txt";
int toStore = 10;
ofstream o(FILENAM,ios::binary);
o.write((char*)&toStore,sizeof(toStore));
o.close();
int toRestore=0;
ifstream i(FILENAM,ios::binary);
i.read((char*)&toRestore,sizeof(toRestore));
cout << toRestore << endl;
return 0;
}