C ++从文件流读取unsigned char [英] C++ reading unsigned char from file stream

查看:2488
本文介绍了C ++从文件流读取unsigned char的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从二进制文件读取无符号字节。
所以我写了以下代码。

  #include< iostream> 
#include< fstream>
#include< vector>
#include< istream>

std :: string filename(file);
size_t bytesAvailable = 128;
size_t toRead = 128;

std :: basic_ifstream< unsigned char> inf(filename.c_str(),std :: ios_base :: in | std :: ios_base :: binary);
if(inF.good())
{
std :: vector< unsigned char> mDataBuffer;
mDataBuffer.resize(bytesAvailable);
inF.read(& mDataBuffer [0],toRead);
size_t counted = inF.gcount();
}

这将导致总是读取0个字节, / p>

似乎有网上的引用,说我需要设置区域设置,使这项工作。



相同的代码使用数据类型char而不是unsigned char



上面的代码使用unsigned char似乎可以在Windows上运行,但是在colinux Fedora 2.6.22.18中运行失败。



解决方案

C ++确实需要实现才能为两个版本的字符特征提供显式特化:

  std :: char_traits< char> 
std :: char_traits< wchar_t>

流和字符串使用这些特征来找出各种各样的东西,比如EOF值,比较字符范围,字符到int的加宽,以及这样的东西。



如果您实例化一个流

  std :: basic_ifstream< unsigned char> 

你必须确保有一个相应的字符trait专业化流可以使用,专业化做有用的事情。此外,流使用构面来进行数字的实际格式化和读取。同样,你必须手动提供那些专业化。该标准甚至不要求实现具有主模板的完整定义。所以你也可能得到一个编译错误:


错误:specialization std :: char_traits无法实例化。

$ b $我将使用 ifstream (这是一个 basic_ifstream< char>



),然后读入向量< char> 。在解释向量中的数据时,以后仍可将它们转换为 unsigned char


I want to read unsigned bytes from a binary file. So I wrote the following code.

#include <iostream>
#include <fstream>
#include <vector>
#include <istream>

std::string filename("file");
size_t bytesAvailable = 128;
size_t toRead = 128;

std::basic_ifstream<unsigned char> inf(filename.c_str(), std::ios_base::in | std::ios_base::binary) ;
if (inF.good())
{
    std::vector<unsigned char> mDataBuffer;
    mDataBuffer.resize(bytesAvailable) ;
    inF.read(&mDataBuffer[0], toRead) ;
    size_t counted = inF.gcount() ;
}

This results in reading in always 0 bytes as shown by the variable counted.

There seem to be references on the web saying that I need to set the locale to make this work. How to do this exactly is not clear to me.

The same code works using the data type 'char' instead of 'unsigned char'

The above code using unsigned char seems to work on Windows but fails running in a colinux Fedora 2.6.22.18 .

What do I need to do to get it to work for linux?

解决方案

C++ does require the implementation only to provide explicit specializations for two versions of character traits:

std::char_traits<char>
std::char_traits<wchar_t>

The streams and strings use those traits to figure out a variety of things, like the EOF value, comparison of a range of characters, widening of a character to an int, and such stuff.

If you instantiate a stream like

std::basic_ifstream<unsigned char>

You have to make sure that there is a corresponding character trait specialization that the stream can use and that this specialization does do useful things. In addition, streams use facets to do actual formatting and reading of numbers. Likewise you have to provide specializations of those too manually. The standard doesn't even require the implementation to have a complete definition of the primary template. So you could aswell get a compile error:

error: specialization std::char_traits could not be instantiated.

I would use ifstream instead (which is a basic_ifstream<char>) and then go and read into a vector<char>. When interpreting the data in the vector, you can still convert them to unsigned char later.

这篇关于C ++从文件流读取unsigned char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆