I want to convert from char to integer, following is the code-
FILE *p; char temp; int temp_int; p=fopen("week3data","r"); temp=getc(p); temp_int=atoi(temp) number in file goes from 1 to 200, need some guidance.
I want to convert from char to integer, following is the code-
FILE *p; char temp; int temp_int; p=fopen("week3data","r"); temp=getc(p); temp_int=atoi(temp) number in file goes from 1 to 200, need some guidance.
If you're reading from a file, you shouldnt be using
temp=getc(p); and if you use
temp=fgetc(p); and the number is, for example 200, you will only read the "2".
so the answer is:
better use
char * buffer; fgets(buffer,10, p); temp_int=atoi(buffer); buffer looks a bit too uninitialized to be honest.