C++ 从文件流中读取无符号字符
我想从二进制文件中读取无符号字节。 所以我写了下面的代码。
#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 个字节,如计数变量所示。
网上似乎有参考资料说我需要设置区域设置才能使其工作。 我不清楚具体如何做到这一点。
相同的代码使用数据类型 'char' 而不是 'unsigned char' 工作。
上面使用 unsigned char 的代码似乎可以在 Windows 上工作,但在 colinux Fedora 2.6.22.18 中运行失败。
我需要做什么才能让它在 Linux 上工作?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
C++ 确实需要实现仅为两个版本的字符特征提供显式专业化:
流和字符串使用这些特征来计算各种事物,例如 EOF 值、字符范围的比较、字符扩展为字符int 之类的东西。
如果您实例化一个流,例如
您必须确保该流可以使用相应的字符特征专门化,并且该专门化确实做了有用的事情。 此外,流使用分面来进行实际的数字格式化和读取。 同样,您也必须手动提供这些专业化。 该标准甚至不要求实现具有主模板的完整定义。 所以你也可能会得到一个编译错误:
我会使用
ifstream
(这是一个basic_ifstream
),然后读取到vector
。 在解释向量中的数据时,您仍然可以稍后将它们转换为unsigned char
。C++ does require the implementation only to provide explicit specializations for two versions of character traits:
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
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:
I would use
ifstream
instead (which is abasic_ifstream<char>
) and then go and read into avector<char>
. When interpreting the data in the vector, you can still convert them tounsigned char
later.不要使用 basic_ifstream,因为它需要专门化。
使用静态缓冲区:
使用向量:
在第一次调用中使用保留而不是调整大小:
如您所见,如果不调用 .resize( counted ),向量的大小将是错误的。 请记住这一点。
这是一种常见的转换方式,请参阅 cppReference
Don't use the basic_ifstream as it requires specializtion.
Using a static buffer:
using a vector:
using reserve instead of resize in first call:
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
如果您使用的是 Windows,则可以直接使用:
在 Linux 上则没有这样的运气,因为未提供 unsigned_char 方面或区域设置,因此请遵循 @Johannes 方法。
If you're on Windows you can directly use:
On Linux no such luck, as unsigned_char facets or locales are not provided, so follow @Johannes approach.
一个更简单的方法:
A much easier way: