我想为占用太多 RAM 的程序执行磁盘 I/O 操作。我使用双精度矩阵并认为将它们作为字节写入磁盘是最快的方法(我需要保留双精度)。
如何做到便携?
我找到了这段代码(here),但作者说它不是可移植的......
#include <iostream>
#include <fstream>
int main()
{
using namespace std;
ofstream ofs( "atest.txt", ios::binary );
if ( ofs ) {
double pi = 3.14;
ofs.write( reinterpret_cast<char*>( &pi ), sizeof pi );
// Close the file to unlock it
ofs.close();
// Use a new object so we don't have to worry
// about error states in the old object
ifstream ifs( "atest.txt", ios::binary );
double read;
if ( ifs ) {
ifs.read( reinterpret_cast<char*>( &read ), sizeof read );
cout << read << '\n';
}
}
return 0;
}