944,130 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 619
  • C RSS
Oct 20th, 2009
0

need help with atoi

Expand Post »
Hi,
can anyone help me to solve this problem?
I have this code:
  1. FILE *in;
  2. char i =fgetc(in);
  3. int perm=0;
  4. perm =atoi(&i);
  5. printf("%d\n", perm);
if first character in my "in" file is 2, the output is 28;
if ------------------------------- is 3, -------------- 38, and so on...

any ideas what is wrong with this code?
thanks
Last edited by WaltP; Oct 20th, 2009 at 2:48 am. Reason: Added CODE tags -- with all the help about them, how could you miss using them????
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hosh is offline Offline
4 posts
since Oct 2009
Oct 20th, 2009
1
Re: need help with atoi
Click to Expand / Collapse  Quote originally posted by hosh ...
Hi,
can anyone help me to solve this problem?
I have this code:

FILE *in;
char i =fgetc(in);
int perm=0;
perm =atoi(&i);
printf("%d\n", perm);

if first character in my "in" file is 2, the output is 28;
if ------------------------------- is 3, -------------- 38, and so on...

any ideas what is wrong with this code?
thanks
atoi() accepts a string as argument. Not a character i as it is in your code, nor a pointer to a char like you give it as in atoi(&i)
Something like:
  1. char twenty_three[] = "23";
  2. int result;
  3.  
  4. result = atoi(twenty_three);
Last edited by Aia; Oct 20th, 2009 at 1:13 am.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Oct 20th, 2009
0
Re: need help with atoi
.
Last edited by hosh; Oct 20th, 2009 at 2:47 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hosh is offline Offline
4 posts
since Oct 2009
Oct 20th, 2009
1
Re: need help with atoi
Quote ...
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?
  1. char c = fgetc(in); /* read character */
  2. if( isdigit(c) ) /* if character is a digit */
  3. 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:
  1. char c = '5';
  2. 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:
  1. int i;
  2. char c = '9';
  3.  
  4. if( isdigit( c ) )
  5. 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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Oct 20th, 2009
0
Re: need help with atoi
Thank you guys.
Now it makes sense...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hosh is offline Offline
4 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Array of Pointers to structures...
Next Thread in C Forum Timeline: Best Windows IDE for C





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC