| | |
need help with atoi
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2009
Posts: 4
Reputation:
Solved Threads: 0
Hi,
can anyone help me to solve this problem?
I have this code:
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
can anyone help me to solve this problem?
I have this code:
c Syntax (Toggle Plain Text)
FILE *in; char i =fgetc(in); int perm=0; perm =atoi(&i); printf("%d\n", perm);
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????
1
#2 Oct 20th, 2009
•
•
•
•
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
i as it is in your code, nor a pointer to a char like you give it as in atoi(&i) Something like:
c Syntax (Toggle Plain Text)
char twenty_three[] = "23"; int result; result = atoi(twenty_three);
Last edited by Aia; Oct 20th, 2009 at 1:13 am.
1
#4 Oct 20th, 2009
•
•
•
•
any ideas what is wrong with this 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?
C Syntax (Toggle Plain Text)
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:
C Syntax (Toggle Plain Text)
char c = '5'; int i = c - '0'; /* i now contains: 5 */
'0' <= character <= '9' You can check on this by using the isdigit() function:
C Syntax (Toggle Plain Text)
int i; char c = '9'; if( isdigit( c ) ) i = c - '0'; /* i now contains: 9 */
Note: you'll need to include the ctype.h header in order to use the isdigit() function.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
![]() |
Similar Threads
- Problem using atoi for my argv argument (C)
- atoi function (C)
- atoi(string.substr(x,x)) Possible? (C++)
- Help with atoi (C++)
- help me about atoi (C++)
- Please help with atoi() (C)
Other Threads in the C Forum
- Previous Thread: Array of Pointers to structures...
- Next Thread: Best Windows IDE for C
| Thread Tools | Search this Thread |
Tag cloud for C
* api append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createcopyoffile createprocess() csyntax database directory drawing dynamic executable execv feet fgets file floatingpointvalidation fork frequency function getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping lowest matrix meter microsoft mqqueue oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked reversing scheduling segmentationfault send single socketprogramming spoonfeeding standard strchr string student suggestions system test testautomation testing unix urboc user whythiscodecausesegmentationfault win32api windowsapi






