I am writing a cache lab in C and I have got the whole input file to print in a char array, but any and all help for my cache lab online uses int to hold the input file, so I am thinking I need that too. I have a three input files. One holds:
10
20
22
18
E10
210
12
I can get 10, 20, 22, 18 to print:
FILE* file;
int address;
file = fopen("address01" , "r");
while (fscanf(file, "%d", &address)) {
printf("%d\n", address);
}
fclose(file);
but it stops after 18 since the next input is a char. I know characters can be held as an int on their own, so how can I also do this with the E and the 10 being together?
CodePudding user response:
You can use the %x
format specifier to read hexadecimal values into an int
. For example:
int address;
if (fscanf(file, "%x", &address) == 1) {
printf("%d\n", address);
}
This will read the hexadecimal value E10
into the int
variable address
.