-
February 17th, 2004, 04:53 PM
#1
fscanf issues (again)
hello i am a bit confused here
all i want to do is open a file and read the first line from it which happens to be a hexidecimal number say C8
it sort of works but the value that is printed is not 200
(which is the decimal equiv. to C8)
int GetValue(FILE* fptr){
int val;
char buff[100];
start = 0;
fgets(buff,95,fptr);
sscanf(buff, "%d", &val);
printf("hex value: %0xd\n", val);
printf*("dec value: %d\n",val);
}
it outputs 10d or 0d
or something else am i not initializing something somewhere?
can someone point to what i am doing wrong here?
-
February 17th, 2004, 05:32 PM
#2
Replace
sscanf(buff, "%d", &val);
with
sscanf(buff, "%x", &val);
Good luck,
Jonas
-
February 19th, 2004, 02:17 PM
#3
%d in scanf tells it to look for a decimal number in the input, not a hexadecimal number.
If trying to read "C8", the scanf would first see the "C", and choke because that isn't a digit, which it is expecting. What it puts into the variable "val" is a good guess. Might be 0, might be unchanged (i.e. garbage in this example.) Might be the number of jelly beans in a jar.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|