C++从二进制文件中写入和读取双精度数
我想要对占用过多 RAM 的程序执行磁盘 I/O 操作。 我使用双精度矩阵,并认为将它们作为字节写入磁盘是最快的方法(我需要保留双精度)。
如何做到便携呢?
我找到了这段代码(此处),但作者说它不可移植。 ..
#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;
}
I want to perform disk I/O operations for a program that takes too much RAM.
I use matrices of doubles and think writing them to disk as bytes is the fastest way (I need to preserve the double precision).
How to do it with portability?
I found this code (here) but the author says it's not portable...
#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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可移植性有不同的定义/级别。如果您所做的只是在一台机器上编写这些代码并在同一台机器上读取它们,那么您唯一关心的可移植性就是该代码是否定义良好。 (是的。)
如果您想跨多个不同平台进行可移植的编写,则需要编写字符串值,而不是二进制值。
但是,请注意,您的代码缺乏正确的错误处理。它不会检查文件是否可以打开并成功写入。
There are different definitions/levels of portability. If all you ever do is to write these on one machine and read it on the same one, the only portability you are concerned with is whether this code is well-defined. (It is.)
If you want to write portably across several different platforms, you need to write string values, rather than binary ones.
However, note that the code you have lacks proper error handling. It doesn't check whether the file could be opened and successfully written to.
我认为仅当您写入文件并在另一台计算机上读取文件时,才会出现可移植性问题。但是,既然您说由于内存限制而想要读/写文件,我只能假设您一次会在一台机器上执行读/写操作。这应该有效。
I think that the portability issue only occurs when you write to a file and read from it on a different machine. However, since you said you want to read/write to a file because of ram limitations, I can only assume you would do read/write operations on one machine at a time. This should work.
他可能指的是一般的二进制表示,而不仅仅是 sizeof。
C - 浮点数(浮点数、双精度数)的序列化< /a>
该规范根本没有指定浮点数的二进制表示形式。 大多数编译器遵循 IEEE,因此如果您了解目标平台,一些单元测试应该可以确保您想要的行为。
He may have been referring to binary representation in general, not just sizeof.
C - Serialization of the floating point numbers (floats, doubles)
The spec doesn't specify binary representation of floating point numbers at all. Most compilers follow IEEE, so a little unit testing should ensure the behavior you want if you know your target platforms.