在二进制模式下需要与T =无符号字符串流的noskipws? [英] Need for noskipws on stream with T=unsigned char in binary mode?
问题描述
感兴趣的代码如下:
typedef unsigned char BYTE;
std :: ifstream文件(文件名,std :: ios :: binary);
file.unsetf(std :: ios :: skipws);
std :: vector< BYTE> VEC;
vec.insert(vec.begin(),
std :: istream_iterator< BYTE>(file),
std :: istream_iterator< BYTE>());
根据Benjamin Lindley的回答, istream ::运算符>>(char)$ c $>为什么std :: istream_iterator忽略换行符? c>跳过空格。但是上面的类型是
unsigned char
,文件是用 std :: binary
打开的。
为什么代码需要显式调用 file.unsetf(std :: ios :: skipws)
(或者, c $ c> file>> std :: noskipws)?
for >>的字符串是:
lockquote
1)跳过空格
2)读取并提取到下一个空格
如果您使用 noskipws
,则第一步被跳过
。
在第一次读取之后,您将被定位在一个空格中,所以下一个(以及所有后续的)读取将立即停止,不会提取任何东西。 b
I came across Reading the binary file into the vector of unsigned chars and tested the code discussed in the question.
The code of interest is below:
typedef unsigned char BYTE;
std::ifstream file(filename, std::ios::binary);
file.unsetf(std::ios::skipws);
std::vector<BYTE> vec;
vec.insert(vec.begin(),
std::istream_iterator<BYTE>(file),
std::istream_iterator<BYTE>());
According to Benjamin Lindley's answer at Why std::istream_iterator ignores newline characters?, istream::operator>>(char)
skips white spaces. But the type above is unsigned char
, and the file was opened with std::binary
.
Why did the code require an explicit call to file.unsetf(std::ios::skipws)
(or alternately, file >> std::noskipws)
?
The basic algorithm for >> of a string is:
1) skip whitespace
2) read and extract until next whitespace
If you use noskipws
, then the first step is skipped
.
After the first read, you are positionned on a whitespace, so the next (and all following) reads will stop immediatly, extracting nothing.
这篇关于在二进制模式下需要与T =无符号字符串流的noskipws?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!