Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 19th, 2005, 08:40 PM
Brian
Guest
 
Posts: n/a
Default Byte I/O

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



  #2  
Old July 19th, 2005, 08:40 PM
Rolf Magnus
Guest
 
Posts: n/a
Default Re: Byte I/O

Brian wrote:
[color=blue]
> 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?[/color]

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*.

  #3  
Old July 19th, 2005, 08:46 PM
Micah Cowan
Guest
 
Posts: n/a
Default Re: Byte I/O

Rolf Magnus <ramagnus@t-online.de> writes:
[color=blue]
> Brian wrote:
>[color=green]
> > 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?[/color]
>
> 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*.[/color]

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.

--
Micah J. Cowan
micah@cowan.name
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles