Joseph Mariadassou
Joseph Mariadassou

Reputation: 126

basic_ofstream<unsigned char> in gcc fails

I have listed below some working code

ofstream of("/home/joe/test.dat", ios::out | ios::binary);
of.write((char*)&dat[0],dat.size());

but the code below produces no output

basic_ofstream<unsigned char> of("/home/joe/test.dat", ios::out | ios::binary);
of.write(&dat[0],dat.size());

I tried with gcc 4.9 on linux. Note that there is no compilation warning or error.

Upvotes: 1

Views: 180

Answers (1)

Alessandro Pezzato
Alessandro Pezzato

Reputation: 8802

This is not working:

basic_ofstream<unsigned char> of("/tmp/test2.dat", ios::out | ios::binary);
of.write(&dat[0],dat.size());  

But this is working:

basic_ofstream<char> of("/tmp/test3.dat", ios::out | ios::binary);
 of.write((char*)&dat[0],dat.size());  

The implementation is under no obligation to supply an instantiation of std::char_traits<unsigned char>, and those that do have incompatible instantiations. It might work with a particular compiler, but it's not required.

Upvotes: 2

Related Questions