C++读取包含 double 类型数字的二进制文件
我有一个包含 double 类型数字的二进制文件。
示例输入文件位于:www.bobdanani.net/download/A.0.0
我想读取该文件并打印其中的数字。 这就是我所做的:
char* buffer;
int length;
string filename = "A.0.0";
ifs.open (filename.c_str(), ios::in | ios::binary);
// get length of file:
ifs.seekg (0, ios::end);
length = ifs.tellg();
ifs.seekg (0, ios::beg);
// allocate memory:
buffer = new char [length];
// read data as a block:
ifs.read (buffer,length);
ifs.close();
cout.write (buffer,length);
cout << buffer << endl;
delete[] buffer;
我还尝试在打印数字时使用类型转换来加倍,但我得到了奇怪的字符。最好的方法是什么?我需要这个二进制文件的数据作为并行程序函数的输入。但这超出了这个问题的范围。
I have a binary file that contains numbers of a type double.
The example input file is available here: www.bobdanani.net/download/A.0.0
I would like to read the file and print the numbers in it.
This is what I have done:
char* buffer;
int length;
string filename = "A.0.0";
ifs.open (filename.c_str(), ios::in | ios::binary);
// get length of file:
ifs.seekg (0, ios::end);
length = ifs.tellg();
ifs.seekg (0, ios::beg);
// allocate memory:
buffer = new char [length];
// read data as a block:
ifs.read (buffer,length);
ifs.close();
cout.write (buffer,length);
cout << buffer << endl;
delete[] buffer;
I have also tried to use a type casting to double when printing the number, but I got strange characters. What is the best way to do this? I need the data of this binary file as an input to a function for a parallel program. But this is out of the scope of this question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然我可能是错的,因为你说数字是用制表符/空格分隔的,所以我愿意这实际上是 ASCII 数据,而不是原始二进制数据。因此,使用浮点值的最佳方法是在
ifstream
对象上使用operator>>
,然后将其推入double
代码>.这会将输入值自动转换为双精度值,而您所做的只是复制组成浮点值的字符字节,但本身不是浮点值。另外,如果您尝试像字符串一样输出缓冲区,则没有显式地以空终止符结束它,因此它将继续读取堆栈,直到遇到空终止符或由于访问内存而出现分段错误操作系统不允许您访问堆栈顶部。但无论哪种方式,最终,您的缓冲区都不会表示 double 数据类型。所以你会得到类似的东西:
While I could be wrong, since you said the number is separated by a tab/space, I'm willing to be this is actually ASCII data, and not raw binary data. Therefore the best way to work with the floating point value would be to use the
operator>>
on theifstream
object and then push that into adouble
. That will do an automatic conversion of the input value into a double, where-as what you've done will merely copy the character bytes that compose a floating point value, but are not a floating point value themselves. Additionaly, if you were trying to output your buffer like a string, you haven't explicitly null-terminated it, so it's going to keep reading up the stack until it encounters a null-terminator or you get a segmentation fault due to accessing memory the OS isn't allowing you to access off the top of the stack. But either way, in the end, your buffer won't be a representation of adouble
data-type.So you would have something like:
不要这样做!上面的代码将把二进制数据转储到标准输出。
也不要这样做!上面的代码会将二进制数据转储到第一个恰好为零的字节到标准输出。如果没有这样的字节,这将继续越过缓冲区的末尾(因此未定义的行为)。
如果缓冲区确实包含双精度数,并且仅包含双精度数,您可以做一些令人讨厌的事情,例如
Don't do this! The above is going to dump the binary data to standard output.
Don't do this either! The above will dump the binary data up to the first byte that happens to be zero to standard output. If there is no such byte, this will just keep on going past the end of the buffer (so undefined behavior).
If the buffer truly does contain doubles, and only doubles, you can do something nasty like
在 C++ 中使用系统函数调用(假设您使用的是 unix 操作系统)并传递
'od -e filename'
作为系统函数调用的参数。然后您可以轻松地通过管道传输它返回的值并读取它们。这是一种方法。当然,还有许多其他方法可以做到这一点。Use the system function call in C++ (assuming you are using unix OS) and pass
'od -e filename'
as the argument of the system function call. And then you can easily pipe the values that it returned and read them. This is one approach. Of course there are many other approaches to do this.