C++,写入向量to ofstream 跳过空格

发布于 2025-01-06 15:02:21 字数 1438 浏览 0 评论 0原文

尽管我尽了最大的努力,但我似乎无法在这里找到错误。我正在向 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

前事休说 2025-01-13 15:02:21

您忘记在 read_file 函数中以二进制模式打开文件。

You forgot to open the file in binary mode in the read_file function.

菩提树下叶撕阳。 2025-01-13 15:02:21

与直接编写向量相比,boost::serialization 是一种更有效的方法,使用boost::archive::binary_oarchive。

Rather than muck around with writing vector<>s directly, boost::serialization is a more effective way, using boost::archive::binary_oarchive.

蔚蓝源自深海 2025-01-13 15:02:21

istream_iterator 在设计上会跳过空格。尝试将您的 std::copy 替换为:

std::copy(
    std::istreambuf_iterator<char>(in),
    std::istreambuf_iterator<char>(),
    std::back_inserter(v));

istreambuf_iterator 直接转到streambuf 对象,这将避免您看到的空白处理。

The istream_iterator by design skips whitespace. Try replacing your std::copy with this:

std::copy(
    std::istreambuf_iterator<char>(in),
    std::istreambuf_iterator<char>(),
    std::back_inserter(v));

The istreambuf_iterator goes directly to the streambuf object, which will avoid the whitespace processing you're seeing.

慢慢从新开始 2025-01-13 15:02:21

我认为“a”被视为新行。我仍然需要考虑如何解决这个问题。

I think 'a' is treated as new line. I still have to think how to get around this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文