Solved

urgent: getc...

Posted on 2008-06-25
4
183 Views
Last Modified: 2010-04-15
hi,

to use getc from file, it is like this

c=getc(f) where f is the filepointer...

my problem is that i want to atoi the value read ....

for example if the value is read 2, i want to read 2 and not ascii 50...

could anyone pls help? i tried c=atoi(getc(f)) which is wrong....
0
Comment
Question by:zizi21
  • 2
4 Comments
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
Something like :
c = getc(f);

int value = -1;

if (isdigit(c)) value = c - '0';

Open in new window

0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
Comment Utility
Or if you are sure that a digit will be read (and don't want to check that), you can of course just do :
int value = getc(f) - '0';

Open in new window

0
 
LVL 24

Expert Comment

by:fridom
Comment Utility
something along hte following lines will do:

Regards
Freidrich

#include <stdio.h>

#include <ctype.h>
 
 

int main (void) {

  int i = getchar();

  int num;
 

  if (isdigit(i)) {

    num = i - '0';

  }

  printf("i = %d, num = %d\n", i , num);
 

  return 0;

}

Open in new window

0
 

Author Comment

by:zizi21
Comment Utility
thanks a million...doing now....
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now