C ++ iostream>>运算符的行为与get()unsigned char不同 [英] C++ iostream >> operator behaves differently than get() unsigned char
问题描述
我正在处理一段代码来做一些压缩,我写了一个比特流类。
我的比特流类跟踪我们正在读取的当前位和当前字节(unsigned char)。
我注意到,从文件读取下一个无符号字符是不同的,如果我使用>>运算符vs get
我只是想知道为什么我得到不同的结果。
ex:
this-> m_inputFileStream.open(inputFile,std :: ifstream :: binary);
unsigned char currentByte;
this-> m_inputFileStream>>> currentByte;
vs。
code> this-> m_inputFileStream.open(inputFile,std :: ifstream :: binary);
unsigned char currentByte;
this-> m_inputFileStream.get((char&)currentByte);
其他信息:
我读的字节是0x0A但是当使用>>它会读它为0x6F
我不知道他们甚至是相关的? (它们不是彼此的2的补码?)
>>运算符也被定义为对无符号字符有效(见 c ++ istream类参考
运算符< < / / code>。你会得到很难跟踪的奇怪的bug,它们对单元测试也是有弹性的,除非你知道要查找什么。读取一个uint8实例将在9失败。
编辑:
#include< iostream>
#include< sstream>
#include< cstdint>
void test(char r){
std :: cout< < r<< std :: endl;
char t ='!';
std :: ostringstream os(std :: ios :: binary);
os< ; r;
if(!os.good())std :: cout<<os not good< std :: endl;
std :: istringstream(os.str(),std :: ios :: binary);
是>> t;
if(!is.good())std :: cout<< 不好< std :: endl;
std :: cout<< std :: hex<< (uint16_t)r
<< vs< std :: hex<< (uint16_t)t < std :: endl;
}
int main(int argc,char ** argv){
test('z');
test('\\\
');
return 0;
}
产生:
测试z
7a vs 7a
测试
不好
a vs 21
我想这从来不会是先验明显的。
I was working on a piece of code to do some compression, and I wrote a bitstream class.
My bitstream class kept track of the current bit we are reading and the current byte (unsigned char).
I noticed that reading the next unsigned character from the file was done differently if I used the >> operator vs get() method in the istream class.
I was just curious why I was getting different results?
ex:
this->m_inputFileStream.open(inputFile, std::ifstream::binary);
unsigned char currentByte;
this->m_inputFileStream >> currentByte;
vs.
this->m_inputFileStream.open(inputFile, std::ifstream::binary);
unsigned char currentByte;
this->m_inputFileStream.get((char&)currentByte);
Additional Info:
To be specific the byte I was reading was 0x0A however when using >> it would read it as 0x6F
I'm not sure how they're even related ? (they're not the 2s complement of each other?)
The >> operator is also defined to work for unsigned char as well however (see c++ istream class reference
解决方案 If you aren't parsing text, don't use operator>>
or operator<<
. You'll get weird bugs that are hard to track down. They are also resilient to unit tests, unless you know what to look for. Reading a uint8 for instance will fail on 9 for instance.
edit:
#include <iostream>
#include <sstream>
#include <cstdint>
void test(char r) {
std::cout << "testing " << r << std::endl;
char t = '!';
std::ostringstream os(std::ios::binary);
os << r;
if (!os.good()) std::cout << "os not good" << std::endl;
std::istringstream is(os.str(), std::ios::binary);
is >> t;
if (!is.good()) std::cout << "is not good" << std::endl;
std::cout << std::hex << (uint16_t)r
<< " vs " << std::hex << (uint16_t)t << std::endl;
}
int main(int argc, char ** argv) {
test('z');
test('\n');
return 0;
}
produces:
testing z
7a vs 7a
testing
is not good
a vs 21
I suppose that would never have been evident a priori.
这篇关于C ++ iostream>>运算符的行为与get()unsigned char不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!