简体   繁体   English

fstream在没有读取位图的情况下跳过字符

[英]fstream skipping characters without reading in bitmap

I am trying to read a bmp file using fstream. 我正在尝试使用fstream读取bmp文件。 However it skips the values between 08 and 0E (hex) for example, for values 42 4d 8a 16 0b 00 00 00 00 00 36 但是它会跳过08和0E(十六进制)之间的值,例如,对于值42 4d 8a 16 0b 00 00 00 00 00 36

it reads 它读

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

skipping 0b like it does not even exist in the document. 跳过0b就好像它甚至不存在于文档中。

What to do? 该怎么办?

code: 码:

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

EDIT: using in.read(a,1); 编辑:使用in.read(a,1); instead of in>>a; 而不是in>>a; solves the reading problem but I need to write unsigned chars and f.write(a,1); 解决了读取问题,但我需要写无符号字符和f.write(a,1); does not accept unsigned chars. 不接受未签名的字符。 Anybody got a function to do the writing with unsigned chars? 有没有人用无符号字符进行写作?

If you want to read the file one byte at a time this is a good use for the istream buffer iterator. 如果要一次读取一个字节的文件,这对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()
                          );
}  

Several issues: 几个问题:

while(!in.eof())
{
    in>>a;
    f<<a;
}

is not the right way to read a file - you need to check if the read worked: 不是读取文件的正确方法 - 您需要检查读取是否有效:

while( in >> a)
{
    f<<a;
}

Secondly, if you want to read a file in binary mode, you need to use the read() and related functions - the >> operator will always perform formatted (non-binary) input, no matter what mode the file is opened in. 其次,如果要以二进制模式读取文件,则需要使用read()和相关函数 - 无论文件在何种模式下打开,>>运算符始终执行格式化(非二进制)输入。

Edit: write will need a cast: 编辑:写需要一个演员:

unsigned char c = 42;
out.write( (char *) & c, 1 );

The operator>> and operator<< are designed for textual input and output. operator>>operator<<设计用于文本输入和输出。

For binary files use read() and write() . 对于二进制文件,使用read()write()

That is because the stream is interpreting the bytes as ascii characters. 这是因为流将字节解释为ascii字符。 08 through 0e are white space (horizontal tab, newline, vertical tab, carriage return, etc.), which are being skipped over. 08到0e是白色空格(水平制表符,换行符,垂直制表符,回车符等),它们被跳过。 Like the other answers said, you need to use the read() method of the stream. 像其他答案所说,你需要使用流的read()方法。

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

int main(int argc, char *argv[])
{
  const char *bitmap;
  const char *output = "s.txt";

  if (argc < 2)
    bitmap = "ben.bmp";
  else
    bitmap = argv[1];

  std::ifstream  in(bitmap, std::ios::in  | std::ios::binary);
  std::ofstream out(output, std::ios::out | std::ios::binary);
  char a;

  while (in.read(&a, 1) && out.write(&a, 1))
    ;

  if (!in.eof() && in.fail())
    std::cerr << "Error reading from " << bitmap << std::endl;

  if (!out)
    std::cerr << "Error writing to "   << output << std::endl;

  return 0;
}

When dealing with binary data, use read() as opposed to the overloaded bitwise operators (<< and >>) 处理二进制数据时,使用read()而不是重载的按位运算符(<<和>>)

If you need to use streams, it is possible to do something like this: 如果您需要使用流,可以执行以下操作:

std::ifstream ifs("bar", std::ios::in | std::ios::binary);
std::ofstream ofs("foo", std::ios::out | std::ios::binary);
ofs << ifs.rdbuf();

Make sure you open it as a binary file like so: 确保将其打开为二进制文件,如下所示:

ifstream ifs('input.bin', ios::in | ios::binary);

ofstream ofs('output.bin', ios::out | ios::binary);

it is also a good practice to check for ifs.good() to make sure everything is ok with the file you opened 检查ifs.good()以确保打开的文件一切正常也是一个好习惯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM