Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 30th, 2004, 7:35 AM   #1
zedoo
Newbie
 
Join Date: Nov 2004
Posts: 2
Rep Power: 0 zedoo is on a distinguished road
hi,

i am trying to read one byte of data from a FILE* like this.

int a = fread((void *)(&(col.r)), sizeof(char), 1, f_origin);


where col.r is a char. fread returns 1, so i suppose the data is read correctly into col.r

1. is this cast to (void *) necessary? could i just write

int a = fread((&(col.r)), sizeof(char), 1, f_origin); ?

2. i am struggling to find an appropriate call to printf to display the char value, for example in hexadecimal or integer. this should be easy..



thanks for your kind answers.
zedoo is offline   Reply With Quote
Old Nov 30th, 2004, 10:00 AM   #2
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
>1. is this cast to (void *) necessary?
No.

>2. i am struggling to find an appropriate call to printf to display the char value, for example in hexadecimal or integer.
printf ( "Decimal: %d\nOctal: %o\nHexadecimal: %x\n", col.r, col.r, col.r );
Eggbert is offline   Reply With Quote
Old Nov 30th, 2004, 12:56 PM   #3
zedoo
Newbie
 
Join Date: Nov 2004
Posts: 2
Rep Power: 0 zedoo is on a distinguished road
so i can use a char wherever i can use ints?

i thought with %d has to follow an int.

felix
zedoo is offline   Reply With Quote
Old Nov 30th, 2004, 2:04 PM   #4
andersRson
Newbie
 
Join Date: Nov 2004
Posts: 16
Rep Power: 0 andersRson is on a distinguished road
Quote:
Originally posted by zedoo@Nov 30 2004, 06:56 PM
i thought with %d has to follow an int.
No, if you use %d with a char you get the ascii-value.
andersRson is offline   Reply With Quote
Old Dec 4th, 2004, 8:41 AM   #5
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
>so i can use a char wherever i can use ints?
>i thought with %d has to follow an int.
char is an integral type with a safe conversion to int. The inverse may not be safe, so be careful when treating int like char.

>No, if you use %d with a char you get the ascii-value.
You get the integral value. Not all systems use ASCII.
Eggbert is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:00 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC