Byte I/O

B

Brian

I'm trying to read a binary file, but the read() function in fstream takes
char*. What I really want are unsigned chars. How can I do this?

Thanks,

--Brian
 
R

Rolf Magnus

Brian said:
I'm trying to read a binary file, but the read() function in fstream
takes char*. What I really want are unsigned chars. How can I do
this?

fstream is just a typedef for basic_fstream<char>, i.e. a char based
stream. Read() takes a pointer to the stream's template argument type.
So if you create your file as basic_fstream<unsigned char>, read() will
take an unsigned char*.
 
M

Micah Cowan

Rolf Magnus said:
fstream is just a typedef for basic_fstream<char>, i.e. a char based
stream. Read() takes a pointer to the stream's template argument type.
So if you create your file as basic_fstream<unsigned char>, read() will
take an unsigned char*.

AIUI, you would then also have to define a specialization of
char_traits<> for unsigned char, as that is not defined by the Standard.

Another common practice seems to be to use
reinterpret_cast<char *>() on a pointer into a buffer of unsigned
char.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,372
Latest member
LucretiaFo

Latest Threads

Top