I'm writing a cache simulator in C and have it all pretty much done...except when I try and scan in addresses fscanf is skipping some of the digits in the hex number: it will only get 4 bytes! If I can't get the right address, the tag bits are incorrect and the simulation won't always work. The task seems pretty straight forward, but I must be missing something. Maybe something to do with fscanf format string idiosyncrasies?
The source file looks like this:
S 00600aa0,1
I 004005b6,5
S 7ff000398,8
M 7ff000390,8
// and so on ...
I have tried using fgets and sscanf instead, but I get the same result.
char buffer[200];
char *pattern = " %c %x,%s\n";
int status; long address; char op;
while ((status = fscanf(source, pattern, &op, &address, buffer)) != EOF) {
if (op != 'I') {
fprintf(stderr,"address: %x\n",address); // DEBUG stmnt
simulate the cache..........................
The debug statement prints out the wrong address for lines 3. Instead of "address: 7ff000398" it writes "address: ff000398". It gets it right for line 1. Why does it only read in the first 4 bytes? 'address' is already a long and I can't find any documentation about %x behaving like this.