any ideas what is wrong with this code?
What exactly is the purpose of your code?
To me it seems like you're reading a file character by character, and then printing out only the digits?
Why doing a conversion to integer if you only want to print the digits out?
char c = fgetc(in); /* read character */
if( isdigit(c) ) /* if character is a digit */
putchar( c ); /* print character */
Also, if you want to do a conversion to integer for only one digit, then you can use a
small trick:
char c = '5';
int i = c - '0'; /* i now contains: 5 */
Please note that you should only apply this trick when you're sure the character variable of which you're subtracting the ASCII value of zero holds a character which is in this range:
'0' <= character <= '9'
You can check on this by using the
isdigit() function:
int i;
char c = '9';
if( isdigit( c ) )
i = c - '0'; /* i now contains: 9 */
But again, in your case it seems to me like you don't need a conversion, as it seems like you're just printing digits on your screen, nothing else.
Note: you'll need to include the
ctype.h header in order to use the
isdigit() function.