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

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';

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.