GCC Bugzilla – Bug 31901
bad_cast when using copy on istreambuf_iterator<unsigned char>
Last modified: 2007-05-11 20:22:26 UTC
When I try and "copy" a basic_ifstream<uint8_t> into a vector<uint8_t> I recieve a bad cast exception. If the uint8_t is replaced with "unsigned char" explicitly the problem persists, but if it is replaced with "char" the problem goes away.
This has been confirmed not only on my system (GCC 4.1.1), but an Ubuntu system running GCC 4.0.3 and another running GCC 4.1.2.
The code compiles and works fine under Microsoft Visual Studio .NET 2005. The g++ problem has not been tested on a Windows system.
typedef unsigned char uint8_t;
using namespace std;
int main(int argc, char **argv)
if (argc != 2)
cout << "Usage: " << argv << " <log filename>" << endl;
// Open the input file
basic_ifstream<uint8_t> inputFile(argv, ios::binary);
cout << "Failed to open the input file" << endl;
// Read the file into a vector
istreambuf_iterator<uint8_t> inputDataStartIterator(inputFile), inputDataEndIterator;
copy(inputDataStartIterator, inputDataEndIterator, back_inserter(inputData));
Command that triggers the bug:
Created attachment 13541 [details]
Preprocessed C++ source code for my test application
This is known and we decided time ago not to do anything about it (you can find something either in Bugzilla or in the libstdc++ mailing list): a portable use of fstream with anything != char and wchar_t requires in general writing a custom codecvt.