c++二进制文件io
c++ binary file io
试图重新工作我的逻辑,以回应这个问题。我决定使用message-size
+ protobuf-object-after-SerializeToArray
对序列化协议缓冲区对象,(如果你不明白我在说什么,不要担心)。无论如何,我的实现不起作用。所以我决定看看c++ fstream是如何工作的。这是一个语义噩梦,我不能确定我是否需要使用seekg
在每次读取后重新定位位置句柄(或者甚至在每次写入后)。我只使用write()和get()方法。下面的人为程序失败了,为什么会失败,在这种情况下我需要seekg吗?
#include <fstream>
#include <boost/cstdint.hpp>
#include <iostream>
void write()
{
boost::uint8_t one = (boost::uint32_t )255;
boost::uint8_t two = (boost::uint32_t) 254;
boost::uint8_t three =(boost::uint32_t) 253;
std::fstream file("test", std::fstream::out | std::fstream::binary | std::fstream::trunc);
file.write( (char *) &one, sizeof(one));
file.write( (char *) &two, sizeof(two));
file.write( (char *) &three, sizeof(two));
std::cout << file.tellg() << std::endl;
file.flush();
file.close();
}
void read()
{
boost::uint8_t one=0;
boost::uint8_t two=0;
boost::uint8_t three=0;
std::fstream file("test", std::fstream::in | std::fstream::binary);
file.get((char *) & one, sizeof(one));
file.get((char *) & two, sizeof(two));
file.get((char *) & three, sizeof(three));
std::cout << file.tellg() << std::endl;
std::cout << (boost::uint32_t) one << ":" << (boost::uint32_t) two << ":" << (boost::uint32_t)three<< std::endl;
file.close();
}
int main()
{
write();
read();
}
输出为:
3
-1
0:0:0
c++二进制文件io让我感到悲伤和愚蠢:(
应该使用istream::read而不是istream::get。
前者提取字符,直到(n - 1)个字符被提取或找到分隔字符为止。
fstream::get()
是针对文本定制的。它期望size参数能够解释缓冲区中的尾空。传递sizeof(one) + 1
作为大小。它也会停止在'n'
上读取。您可以更改被视为分隔符的字符,但似乎不能使用"no delimiter, please"。如果需要原始二进制数据,请使用fstream::read()
。
当读取单个字节时,也可以使用
one = (boost::uint8_t) file.get();
two = (boost::uint8_t) file.get();
three = (boost::uint8_t) file.get();
但是对于大小> 1的数据来说,这自然是不好的。你需要fstream::read()
。
file.read((char *) & one, sizeof(one));
file.read((char *) & two, sizeof(two));
file.read((char *) & three, sizeof(three));
结果:3
3
255:254:253
istream::get
不是用于二进制I/O,而是用于文本I/O。特别是,file.get(ptr, n)
读取一个C字符串,即最多只读取n-1个字符,然后以null结束。此外,如果您在流中遇到'n'
(这不是您想要的二进制I/O),则读取将停止。请注意,如果您正在检查流状态(在执行I/O时总是一个好主意),您会发现第一次读取尝试已经导致错误。
您应该使用read
和write
进行二进制I/O(或者直接使用相应的流缓冲区)。
- 正在读取二进制文件(is_open)
- 在C++中将类(带有Vector成员)保存为二进制文件
- 如何从二进制文件中读取字符串
- 保存/加载大量短数组到二进制文件
- 从二进制文件中读取整数数组
- Android 在编译二进制文件时重建静态库
- 在 C++ 中将双精度变量写入二进制文件
- clang 的 libFuzzer 可以在同一二进制文件中测试超过 1 个 API 吗?
- C++:实际上不是从二进制文件中读取
- 如何从二进制文件中的给定符号中获取调用程序图
- 将内部带有矢量的结构保存/读取到二进制文件中
- 编译多个C++文件.调用二进制文件以运行代码
- 如何使用位字段将数据从二进制文件复制到结构中?
- uint8_t同一二进制文件的不同十进制值
- C++单个生成文件多个二进制文件
- 尝试将数字写入二进制文件时引发异常
- C++中读/写二进制文件
- C++三维模型io、二进制文件读取(badbit)和#DEN问题
- c++二进制文件io
- C++ IO 二进制文件流:未指定输出时的默认值