Solved

urgent: getc...

Posted on 2008-06-25
4
184 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
ID: 21863742
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
ID: 21863749
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
ID: 21863788
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
ID: 21863887
thanks a million...doing now....
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

920 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

16 Experts available now in Live!

Get 1:1 Help Now