English 中文(简体)
f 流ski特性,没有读写
原标题:fstream skipping characters without reading in bitmap

I am trying to read a bmp file using fstream. 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

案文如下:

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

该文件中甚至没有像现在这样ski。

什么?

法典:

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

EDIT:使用in.read(a,1);,而不是in>>a;解决阅读问题,但我需要书写未签名的果园和f.write(a,1);不接受未签名的果园。 任何人都有责任与未签名的char做书写?

最佳回答
#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;
}
问题回答

如果你想在一流缓冲器使用时逐字阅读档案。

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

The operator>> and operator<< are designed for textual content and production.

http://www.un.org/Depts/DGACM/index_french.htm

几个问题:

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

不是阅读档案的正确途径——如果阅读的话,需要检查:

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

第二,如果你想阅读双向文件,你需要使用“(......)”和相关功能——“the >>操作者将始终进行格式化(非二元)投入,而不论档案的开启方式如何。

<<>Edit>:

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

这是因为,溪流将 by物解释为乙 character。 8至0的白色空间(横向tab、新线、垂直表、运输回来等)正在ski。 与其他答复一样,你需要使用上游的“()”方法。

在处理双向数据时,使用()而不是超载双向操作员(加固、加固;和“特”);

如果你们需要使用溪流,就能够做这样的事情:

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

确保你作为双向文件打开:

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

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

检查<代码>ifs.good(),以确保一切都与你开立的档案吻合,也是一种良好做法。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签