C++ 从文件流中读取无符号字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/604431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:12:41  来源:igfitidea点击:

C++ reading unsigned char from file stream

c++file-iostreamifstream

提问by David

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.

这导致读取始终为 0 字节,如变量计数所示。

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'

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

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

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

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

我需要做什么才能让它在 linux 上工作?

回答by Johannes Schaub - litb

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

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

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.

流和字符串使用这些特征来计算出各种各样的东西,比如 EOF 值、一系列字符的比较、将字符扩展为 int 等等。

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:

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

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

错误:无法实例化专业化 std::char_traits。

I would use ifstreaminstead (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 charlater.

我会ifstream改用(这是 a basic_ifstream<char>)然后去读入 a vector<char>。在解释向量中的数据时,您仍然可以将它们转换为unsigned char稍后的。

回答by sfossen

Don't use the basic_ifstream as it requires specializtion.

不要使用 basic_ifstream,因为它需要专业化。

Using a static buffer:

使用静态缓冲区:

linux ~ $ cat test_read.cpp
#include <fstream>
#include <iostream>
#include <vector>
#include <string>


using namespace std;

int main( void )
{
        string filename("file");
        size_t bytesAvailable = 128;

        ifstream inf( filename.c_str() );
        if( inf )
        {
                unsigned char mDataBuffer[ bytesAvailable ];
                inf.read( (char*)( &mDataBuffer[0] ), bytesAvailable ) ;
                size_t counted = inf.gcount();
                cout << counted << endl;
        }

        return 0;
}
linux ~ $ g++ test_read.cpp
linux ~ $ echo "123456" > file
linux ~ $ ./a.out
7

using a vector:

使用向量:

linux ~ $ cat test_read.cpp

#include <fstream>
#include <iostream>
#include <vector>
#include <string>


using namespace std;

int main( void )
{
        string filename("file");
        size_t bytesAvailable = 128;
        size_t toRead = 128;

        ifstream inf( filename.c_str() );
        if( inf )
        {

                vector<unsigned char> mDataBuffer;
                mDataBuffer.resize( bytesAvailable ) ;

                inf.read( (char*)( &mDataBuffer[0]), toRead ) ;
                size_t counted = inf.gcount();
                cout << counted << " size=" << mDataBuffer.size() << endl;
                mDataBuffer.resize( counted ) ;
                cout << counted << " size=" << mDataBuffer.size() << endl;

        }

        return 0;
}
linux ~ $ g++ test_read.cpp -Wall -o test_read
linux ~ $ ./test_read
7 size=128
7 size=7

using reserve instead of resize in first call:

在第一次调用中使用保留而不是调整大小:

linux ~ $ cat test_read.cpp

#include <fstream>
#include <iostream>
#include <vector>
#include <string>


using namespace std;

int main( void )
{
        string filename("file");
        size_t bytesAvailable = 128;
        size_t toRead = 128;

        ifstream inf( filename.c_str() );
        if( inf )
        {

                vector<unsigned char> mDataBuffer;
                mDataBuffer.reserve( bytesAvailable ) ;

                inf.read( (char*)( &mDataBuffer[0]), toRead ) ;
                size_t counted = inf.gcount();
                cout << counted << " size=" << mDataBuffer.size() << endl;
                mDataBuffer.resize( counted ) ;
                cout << counted << " size=" << mDataBuffer.size() << endl;

        }

        return 0;
}
linux ~ $ g++ test_read.cpp -Wall -o test_read
linux ~ $ ./test_read
7 size=0
7 size=7

As you can see, without the call to .resize( counted ), the size of the vector will be wrong. Please keep that in mind. it is a common to use casting see cppReference

如您所见,如果不调用 .resize( counted ),向量的大小将是错误的。请记住这一点。使用强制转换很常见,请参阅cppReference

回答by rlbond

A much easier way:

一个更简单的方法:

#include <fstream>
#include <vector>

using namespace std;


int main()
{
    vector<unsigned char> bytes;
    ifstream file1("main1.cpp", ios_base::in | ios_base::binary);
    unsigned char ch = file1.get();
    while (file1.good())
    {
        bytes.push_back(ch);
        ch = file1.get();
    }
    size_t size = bytes.size();
    return 0;
}