c++ - fstream 跳过字符而不读取位图

标签 c++ bitmap fstream

我正在尝试使用 fstream 读取 bmp 文件。 但是它会跳过 08 和 0E(十六进制)之间的值 例如,对于值 42 4d 8a 16 0b 00 00 00 00 00 36

上面写着

42 4d 8a 16 00 00 00 00 00 36

跳过 0b,就像它甚至不存在于文档中一样。

怎么办?

代码:

ifstream in;
in.open("ben.bmp", ios::binary);
unsigned char a='\0';
ofstream f("s.txt");
while(!in.eof())
{
    in>>a;
    f<<a;
}

编辑:使用 in.read(a,1); 而不是 in>>a; 解决了读取问题,但我需要编写无符号字符和 f.write(a,1); 不接受无符号字符。有人有用无符号字符进行写作的功能吗?

最佳答案

如果你想一次一个字节地读取文件,这是 istream 缓冲区迭代器的一个很好的用途。

int main()
{
   ifstream in("ben.bmp", ios::binary);  
   ofstream f("s.txt");  

   //
   // Note: this is NOT istream_iterator
   // The major different is that the istreambuf_iterator does not skip white space.
   //
   std::istreambuf_iterator<char>  loop(in);
   std::istreambuf_iterator<char>  end;

   for(; loop != end; ++loop)
   {
       f << (*loop);  
   }

   // You could now also use it with any of the std algorithms
       // Alternative to Copy input to output
            std::copy(loop,end,std::ostream_iterator<char>(f));

       // Alternative if you want to do some processing on each element
       // The HighGain_FrequencyIvertModulator is a functor that will manipulate each char.
            std::transform( loop,
                            end,
                            std::ostream_iterator<char>(f),
                            HighGain_FrequencyIvertModulator()
                          );
}  

关于c++ - fstream 跳过字符而不读取位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2031007/

相关文章:

c++ - 我们可以重新定义正则表达式吗?

c++ - 令人困惑的输出

C++ ofstream 仅从 ifstream 输出最后一行数据

c++ - 在 C++ 中防止文件中的行重复

c++ - 在 C++ 中的文件名中使用的 int 的位数

c++ - 声明为 void 的变量或字段

C++ 函数在 double 组中查找最大值?

android - 如何绘制平滑的位图路径,没有任何中断

vba - 如何使用 pastepecial 将图表作为位图粘贴到 vba 中的另一张纸上

java - Android位图和捕获屏幕