代码之家  ›  专栏  ›  技术社区  ›  Khaled Alshaya

如何将basic_filebuf与char以外的元素类型一起使用?

  •  4
  • Khaled Alshaya  · 技术社区  · 17 年前

    假设我想使用 basic_filebuf . 我有一种叫做 boost::uintmax_t 它的大小是 8 bytes

    typedef basic_filebuf<uintmax_t> file;
    typedef istreambuf_iterator<uintmax_t> ifile;
    
    file f;
    vector<uintmax_t> data, buf(2);
    
    f.open("test.txt", std::ios::in | std::ios::binary);
    f.pubsetbuf(&buf[0], 1024);
    
    ifile start(&f), end;
    while(start != end)
    {
        data.push_back(*start);
        start++;
    }
    

    问题是一些 bytes 得到阅读,其他人不会。例如,假设有 9 bytes 1-9 :

    |1|2|3|4|5|6|7|8|9|
    

    data ,其中包含 4 bytes 仅从中的原始数据 f :

    [0|0|0|0|4|3|2|1] --> only element in [data]
    

    我做错了什么?这是我第一次使用 基本文件 直接,虽然我知道如何使用 filebuf .

    2 回复  |  直到 17 年前
        1
  •  4
  •   Éric Malenfant    17 年前

    基本的_filebuf处理“内部”字符类型和“外部”字符类型。“外部”是文件的内容,总是字节。“内部”参数是模板参数,用于与程序的接口。为了在两者之间转换,basic_filebuf使用其区域设置的codecvt方面。

    • 使用仅在“内部”和“外部”编码之间进行强制转换的“退化”编解码器VT,而不是尝试执行转换。
    • 使用basic_filebuf,确保使用“经典”语言环境,并自己进行字符转换
        2
  •  0
  •   Jonathan Graehl    17 年前

    basic_filebuf类型的对象是 创建的内部缓冲区为 键入char*,而不考虑 参数元素。这意味着 Unicode字符串(包含wchar\u t 字符)将转换为 ANSI字符串(包含字符) 字符),然后再写入 内部缓冲区。存储Unicode 在缓冲区中,创建一个新的 wchar\u t类型的缓冲区,并将其设置为 方法

    从…起 http://msdn.microsoft.com/en-us/library/tzf8k3z8(VS.80).aspx