テキスト ファイルを使用する必要がある場合は、書式設定された値の区切りを示す何かが必要です。スペースの例:
ofstream odt;
odt.open("example.dat");
for (uint64_t i = 0 ; i < 100 ; i++)
odt << i << ' ';
odt.flush() ;
ifstream idt;
idt.open("example.dat");
uint64_t cur;
while( idt >> cur )
cout << cur << ' ';
そうは言っても、低レベルの iostream メソッド ( 、) を使用し、これらをバイナリで記述することを強くお勧めします。write()read()
読み取り/書き込みおよびバイナリ データを使用したサンプル (64 ビットの htonl/ntohl と同等のものはありますか??)
ofstream odt;
odt.open("example.dat", ios::out|ios::binary);
for (uint64_t i = 0 ; i < 100 ; i++)
{
uint32_t hval = htonl((i >> 32) & 0xFFFFFFFF);
uint32_t lval = htonl(i & 0xFFFFFFFF);
odt.write((const char*)&hval, sizeof(hval));
odt.write((const char*)&lval, sizeof(lval));
}
odt.flush();
odt.close();
ifstream idt;
idt.open("example.dat", ios::in|ios::binary);
uint64_t cur;
while( idt )
{
uint32_t val[2] = {0};
if (idt.read((char*)val, sizeof(val)))
{
cur = (uint64_t)ntohl(val[0]) << 32 | (uint64_t)ntohl(val[1]);
cout << cur << ' ';
}
}
idt.close();