Search code examples
c++ccharfstreamunsigned-char

Reading binary data with fstream method


I need to read binary data to buffer, but in the fstreams I have read function reading data into char buffer, so my question is:

How to transport/cast binary data into unsigned char buffer and is it best solution in this case?

Example

  char data[54];
  unsigned char uData[54];
  fstream file(someFilename,ios::in | ios::binary);
  file.read(data,54);
  // There to transport **char** data into **unsigned char** data (?)
  // How to?

Solution

  • Just read it into unsigned char data in the first place

    unsigned char uData[54];
    fstream file(someFilename,ios::in | ios::binary);
    file.read((char*)uData, 54);
    

    The cast is necessary but harmless.