Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Printf And Fread (http://www.programmingforums.org/showthread.php?t=1355)

zedoo Nov 30th, 2004 8:35 AM

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.

Eggbert Nov 30th, 2004 11:00 AM

>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 );

zedoo Nov 30th, 2004 1:56 PM

so i can use a char wherever i can use ints?

i thought with %d has to follow an int.

felix

andersRson Nov 30th, 2004 3:04 PM

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.

Eggbert Dec 4th, 2004 9:41 AM

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


All times are GMT -5. The time now is 2:33 AM.

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