【问题标题】:How to force stream to write numeric data according to type如何强制流根据类型写入数字数据
【发布时间】:2014-10-30 14:25:43
【问题描述】:

我使用wofstream 来合并文件并写入 wstring 和整数(直接来自内存)。我的问题(目前)是假设我有int32,其中包含号码7。在内存中它需要 4 个字节,但是当我将它写入流并使用十六进制查看器检查时,我只看到了这个 -- 7(单字节)。

当然这对我没有用,因为我必须阅读它并且可靠地阅读它。

那么如何让stream根据type来写数据--所以2 bytes类型要2 bytes,4 bytes-- 4 bytes,以此类推?

记录一下我的合并功能:

for (const std::wstring &fn : files)  
{
    std::wifstream is_src(dst_dir+fn, std::ios_base::binary);

    os_dest << is_src.rdbuf();
    os_dest << fn;
    os_dest << static_cast<__int32>(fn.size());
}

os_dest << static_cast<__int32>(files.size());

我添加了强制转换以 100% 确定有 4 个字节,仅此而已。

【问题讨论】:

    标签: c++ stream


    【解决方案1】:

    在这里找到答案:writing integer to binary file (C++)

    关键不是使用流操作符,而是方法write

    【讨论】:

      猜你喜欢
      • 2020-03-18
      • 2012-04-09
      • 1970-01-01
      • 1970-01-01
      • 2020-08-01
      • 1970-01-01
      • 2018-06-24
      • 2011-01-04
      • 2017-02-19
      相关资源
      最近更新 更多