0

Hello all:

I successfully use (int)i=fgetc(stream) to read data from a file. But the problem is:

All the data in the file are integers, like 3 0 2 3 0 0 1. (with a space between every two integers)

Code:

for(i=0;(i<100)&&((n=fgetc(stream))!=EOF)&&(ch!='\n');i++)
    {
        buffer[i] = n;
        printf("%d=%d    ",i, buffer[i]);  /*For debugging purpose only*/
    }
    numVerts = buffer[0];

The result I get for numVerts is actually 53, which is supposed to be 3.
Can anyone help me with this?

Thanks

Edited by pyTony: fixed formating

2
Contributors
3
Replies
4
Views
11 Years
Discussion Span
Last Post by Ancient Dragon
0

you are confusing the ascii value of '3' (numeric value is 53) with the binary value of 3 (decimal value of 3). Look at an ascii chart and it will show you the ascii values of all 255 possible values that can be contained in unsigned char.

If you want the decimal value of 3, then you have to convert '3' to int value -- simply subtract the ascii value of '0'
numVerts = buffer[0] - '0';

0

Thanks dragon. I thought all the dragon were too proud to talk to humans. :-) You are the most friendly dragon I have ever met. :-)

0

Thanks dragon. I thought all the dragon were too proud to talk to humans. :-) You are the most friendly dragon I have ever met. :-)

:mrgreen: :mrgreen: :mrgreen: But don't forget -- ancient does not necessarily mean all-knowing.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.