C++,写入向量to ofstream 跳过空格
尽管我尽了最大的努力,但我似乎无法在这里找到错误。我正在向 ofstream 写入一个向量。该向量包含二进制数据。然而,由于某种原因,当应该写入空白字符(0x10、0x11、0x12、0x13、0x20)时,它会被跳过。
我尝试过使用迭代器和直接 ofstream::write()。
这是我正在使用的代码。我已经注释掉了我尝试过的一些其他方法。
void
write_file(const std::string& file,
std::vector<uint8_t>& v)
{
std::ofstream out(file, std::ios::binary | std::ios::ate);
if (!out.is_open())
throw file_error(file, "unable to open");
out.unsetf(std::ios::skipws);
/* ostreambuf_iterator ...
std::ostreambuf_iterator<char> out_i(out);
std::copy(v.begin(), v.end(), out_i);
*/
/* ostream_iterator ...
std::copy(v.begin(), v.end(), std::ostream_iterator<char>(out, ""));
*/
out.write((const char*) &v[0], v.size());
}
编辑:以及读回它的代码。
void
read_file(const std::string& file,
std::vector<uint8_t>& v)
{
std::ifstream in(file);
v.clear();
if (!in.is_open())
throw file_error(file, "unable to open");
in.unsetf(std::ios::skipws);
std::copy(std::istream_iterator<char>(in), std::istream_iterator<char>(),
std::back_inserter(v));
}
这是一个示例输入:
30 0 0 0 a 30 0 0 0 7a 70 30 0 0 0 32 73 30 0 0 0 2 71 30 0 0 4 d2
这是我读回时得到的输出:
30 0 0 0 30 0 0 0 7a 70 30 0 0 0 32 73 30 0 0 0 2 71 30 0 0 4 d2
如您所见,0x0a 被省略,表面上是因为它是空格。
任何建议将不胜感激。
Despite my sincerest efforts, I cannot seem to locate the bug here. I am writing a vector to an ofstream. The vector contains binary data. However, for some reason, when a whitespace character (0x10, 0x11, 0x12, 0x13, 0x20) is supposed to be written, it is skipped.
I have tried using iterators, and a direct ofstream::write().
Here is the code I'm using. I've commented out some of the other methods I've tried.
void
write_file(const std::string& file,
std::vector<uint8_t>& v)
{
std::ofstream out(file, std::ios::binary | std::ios::ate);
if (!out.is_open())
throw file_error(file, "unable to open");
out.unsetf(std::ios::skipws);
/* ostreambuf_iterator ...
std::ostreambuf_iterator<char> out_i(out);
std::copy(v.begin(), v.end(), out_i);
*/
/* ostream_iterator ...
std::copy(v.begin(), v.end(), std::ostream_iterator<char>(out, ""));
*/
out.write((const char*) &v[0], v.size());
}
EDIT: And the code to read it back.
void
read_file(const std::string& file,
std::vector<uint8_t>& v)
{
std::ifstream in(file);
v.clear();
if (!in.is_open())
throw file_error(file, "unable to open");
in.unsetf(std::ios::skipws);
std::copy(std::istream_iterator<char>(in), std::istream_iterator<char>(),
std::back_inserter(v));
}
Here is an example input:
30 0 0 0 a 30 0 0 0 7a 70 30 0 0 0 32 73 30 0 0 0 2 71 30 0 0 4 d2
And this is the output I am getting when I read it back:
30 0 0 0 30 0 0 0 7a 70 30 0 0 0 32 73 30 0 0 0 2 71 30 0 0 4 d2
As you can see, 0x0a is being ommited, ostensibly because it's whitespace.
Any suggestions would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您忘记在 read_file 函数中以二进制模式打开文件。
You forgot to open the file in binary mode in the read_file function.
与直接编写向量相比,boost::serialization 是一种更有效的方法,使用boost::archive::binary_oarchive。
Rather than muck around with writing vector<>s directly,
boost::serialization
is a more effective way, usingboost::archive::binary_oarchive
.istream_iterator 在设计上会跳过空格。尝试将您的 std::copy 替换为:
istreambuf_iterator 直接转到streambuf 对象,这将避免您看到的空白处理。
The istream_iterator by design skips whitespace. Try replacing your std::copy with this:
The istreambuf_iterator goes directly to the streambuf object, which will avoid the whitespace processing you're seeing.
我认为“a”被视为新行。我仍然需要考虑如何解决这个问题。
I think 'a' is treated as new line. I still have to think how to get around this.