无法从文件中读取简单的二进制整数? (C++)
我的代码简单如下:
更新:
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char **argv)
{
ifstream r("foo.bin", ios::binary);
ofstream w("foo.bin", ios::binary);
int i;
int ints[10] = {0,1,2,3,4,5,6,8,9};
w.write((char*)&ints, sizeof(ints));
int in_ints[10];
r.read((char*)&in_ints, sizeof(in_ints));
for(i = 0;i < 10;i++) {
cout << in_ints[i] << " ";
}
cout << endl;
return 0;
}
现在,写入部分似乎是成功的,例如运行 32 位长的 od 命令(我的系统是 32 位)将显示正确的序列,包括十六进制转储。
然而,阅读时,我得到了不应该发生的随机序列和负整数(它被分割,并且大部分为零,因为我的整数很小,符号位不应该打开。)
你明白为什么我的读取方法无法工作吗,当它真的与我的写方法相反时?
My code is simply as this:
UPDATED:
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char **argv)
{
ifstream r("foo.bin", ios::binary);
ofstream w("foo.bin", ios::binary);
int i;
int ints[10] = {0,1,2,3,4,5,6,8,9};
w.write((char*)&ints, sizeof(ints));
int in_ints[10];
r.read((char*)&in_ints, sizeof(in_ints));
for(i = 0;i < 10;i++) {
cout << in_ints[i] << " ";
}
cout << endl;
return 0;
}
Now, the write portion appears to be successful, for example running the od command with 32 bit longs (my system is 32 bit) will display the correct sequence, including a hex dump.
Reading however, I get random sequences such and negative integers that should not happen (it is split up, and mostly zeros as my integers are small, the sign bits should not be on.)
Do you see why my read method has failed to work, when it is really an opposite of my write method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在
r.read
之前尝试w.flush()
或w.close()
。问题是,当您编写它时,它通常会缓冲文本并且不会将其保存在文件中。所以 r.read 文件中实际上没有任何内容。try
w.flush()
orw.close()
beforer.read
. the problem is when you write it usually bufferes text and doesn't save it in file. so there is nothing realy in file forr.read
.此代码:
打印:
0 1 2 3 4 5 6 8 9 0
请注意,您的初始化中缺少一些数字。
This code:
prints:
0 1 2 3 4 5 6 8 9 0
Note there are some numbers missing from your initialisation.
写入文件的数据不一定对其他流可见(在
相同的过程或不同)直到文件被关闭,或至少刷新。
如果在启动程序之前文件
"foo.bin"
不存在,打开它进行阅读将会失败(因此所有进一步的使用都将是
无操作)。你的程序违反了最基本的规则之一
编程:如果有任何事情可能失败,请务必检查它以前没有发生过
继续。您不检查 IO 的任何结果。
我还可以补充一点,这种方式读取和写入数据非常非常
脆弱,一般只能保证在相同的范围内工作
过程 - 甚至使用不同版本的编译器重新编译
或者不同的编译器选项可能会导致数据表示
更改,结果将无法读取写入的数据
早些时候。当然,对于
int
数组来说,这是非常重要的。只要不改变机器架构就不太可能。但在
一般来说,如果您将来需要重新读取数据,这是一个
最好避免的技术。 (
(char*)
是reinterpret_cast
,并且作为我们知道,
reinterpret_cast
是一个非常强烈的信号,表明存在可移植性问题。)
Data written to a file isn't necessarily visible to other streams (in
the same process or not) until the file is closed, or at least flushed.
And if the file
"foo.bin"
doesn't exist before you start the program,opening it for reading will fail (and so all further use will be a
no-op). Your program violates one of the most basic rules of
programming: if anything can fail, always check that it hasn't before
continuing. You don't check any of the results of your IO.
I might also add that reading and writing data this way is very, very
fragile, and can generally only be guaranteed to work within the same
process—even recompiling with a different version of the compiler
or different compiler options may cause the data representation to
change, with the results that you won't be able to read data written
earlier. In the case of arrays of
int
, of course, this is highlyunlikely as long as you don't change machine architectures. But in
general, if you'll need to reread the data in the future, it's a
technique best avoided. (The
(char*)
arereinterpret_cast
, and aswe know,
reinterpret_cast
is a very strong signal that there is aportability issue.)