I want to read unsigned bytes from a binary file. So I wrote the following code.
#include <iostream>
#include <fstream>
#include <vector>
#include <istream>
std::string filename("file");
size_t bytesAvailable = 128;
size_t toRead = 128;
std::basic_ifstream<unsigned char> inf(filename.c_str(), std::ios_base::in | std::ios_base::binary) ;
if (inF.good())
{
std::vector<unsigned char> mDataBuffer;
mDataBuffer.resize(bytesAvailable) ;
inF.read(&mDataBuffer[0], toRead) ;
size_t counted = inF.gcount() ;
}
This results in reading in always 0 bytes as shown by the variable counted.
There seem to be references on the web saying that I need to set the locale to make this work. How to do this exactly is not clear to me.
The same code works using the data type 'char' instead of 'unsigned char'
The above code using unsigned char seems to work on Windows but fails running in a colinux Fedora 2.6.22.18 .
What do I need to do to get it to work for linux?
typedef basic_string<char> string;
, so you can always make an unsigned char string class a latypedef basic_string<unsigned char> bytestring;
. – Floeter