Fstream skips non-read characters in a bitmap

I am trying to read a bmp file using fstream. However, it skips values ​​between 08 and 0E (hex), for example, for values ​​42 4d 8a 16 0b 00 00 00 00 36

he reads

42 4d 8a 16 00 00 00 00 36

skip 0b as it doesn't exist at all in the document.

What to do?

the 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);instead in>>a;solves the reading problem, but I need to write unsigned characters, and f.write(a,1);not accept unsigned characters. Has anyone got a function for writing with unsigned characters?

+3
source share
7 answers
#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;
}
+2
source

, istream buffer.

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()
                          );
}  
+7

:

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

- , :

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

-, , read() . β†’ ( ) , , .

: :

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

operator>> operator<< .

read() write().

+6

, ascii. 08 0e - ( , , , ..), . , read() .

+4

read() (< β†’ )

, - :

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

, :

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

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

ifs.good(), ,

0

Source: https://habr.com/ru/post/1727815/


All Articles