0

My code does not happen any error, it seems in my opinion my fopen() in FILE* file= fopen("008.espresso.din", "r"); can not open the file that i'm using, if it does not happen any error, i don't know what else to do. why i can't open the file ?, my data is like this :

2 400170 8fa40000
0 7ffebc84 0
2 400174 3c1c1001
2 400178 279cc660
2 40017c 27a50004
2 400180 af8589a8

i compile it with gcc -o cache cache.c -lm

here is the code :

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* struktur baca */
struct read
{
    int dummy1;
    int address;
    int dummy2;
}read;

/* struktur memory address */
struct memaddress
{
    int tag;
    int set;
}mem;

/*struktur cache */
struct cache
{
    int tag;
    int valid;
};

int main(void)
{
    int i;
    double hitrate;
    double missrate;
    int maskingsetbit;
    struct cache* cache;
    int set=0, bitset;
    int block, bitblock;
    int miss=0;
    int hit=0;
    int total=0;

    /* jumlah cache dan block */
    do{
        printf("Masukkan jumlah set cache:");   
        scanf("%d", &set);
        printf("Masukkan jumlah block:");
        scanf("%d", &block);

        /* hitung jumlah bit */ 
        bitset=log2(set);
        bitblock=log2(block*4);
        maskingsetbit=pow(2,bitset)-1;

        /* periksa jumlah bit */
    }while (bitset+bitblock>31);

    /*alokasi memory pada cache */
    cache=(struct cache*) malloc(set*sizeof(struct cache));

    /* buka file */
    FILE* file= fopen("008.espresso.din", "r");

    /* inisialisasi cache */
    for(i=0;i<set;i++)
    {
        cache[i].valid=0;
        cache[i].tag=0;
    }
    while (!feof(file))
    {
        /* baca data dinero */
        fscanf(file, "%x", &read.dummy1);
        if(feof(file))  
            break;
        fscanf(file, "%x", &read.address);  
        fscanf(file, "%x", &read.dummy2);

        /*pilah bit tag dari address */
        mem.tag = read.address>>(bitblock+bitset);
        mem.set = (read.address>>bitblock)&(maskingsetbit);

        /*hitung total reference */
        total++;

        if(cache[mem.set].valid==0)
        {
            miss++;
            cache[mem.set].valid=1;
            cache[mem.set].tag=mem.tag;
        }else{
            if(cache[mem.set].tag!=mem.tag)
            {
                miss++; 
                cache[mem.set].tag!=mem.tag;
            }else{
                hit++;
            }
        }

        /* hitung hit rate dan miss rate */
        hitrate=hit;
        hitrate=hitrate/total*100;
        missrate=miss;
        missrate=missrate/total*100;

        printf("\n\nTotal Hit\t=%d\n",hit);
        printf("Total Miss\t=%d\n",miss);
        printf("Hitt Rate\t=%.2f%%\n",hitrate);
        printf("Miss Rate\t=%.2f%%\n",missrate);
        printf("Total Reference\t=%d\n",total);

        fclose(file);
        return(0);

    }
}
nalzok
  • 14,965
  • 21
  • 72
  • 139
mas bro
  • 312
  • 1
  • 12
  • 2
    Do check the return value of `fopen()`. Also note that [they say you shouldn't cast the result of `malloc()` in C](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – MikeCAT Mar 03 '16 at 02:39
  • 1
    I see two things that look suspicious. "cache[mem.set].tag!=mem.tag;" has no effect. Did you mean to use "|="? Also, the code at the end that does some calculations and prints out results is inside the while loop. Is it supposed to be outside? – Andy Schweig Mar 03 '16 at 02:50
  • ok first @MikeCAT i did cast the result, i add this : `if(NULL == file) { printf("\n fopen() Error!!!\n"); return 1; } printf("\n File opened successfully through fopen()\n");` , and it says : `File opened successfully through fopen()` , so that does mean that i dont have problem open the file ? – mas bro Mar 03 '16 at 02:57
  • and for @AndySchweig i intend to use != because if it is the tag not same then it miss, else hit, owh maybe i suppose to put it outside, i'll try it, – mas bro Mar 03 '16 at 02:57
  • I meant the "cache[mem.set].tag!=mem.tag;" on a line by itself. That's a comparison but you're not doing anything with the result. – Andy Schweig Mar 03 '16 at 03:00
  • 1
    Also check return values of `fscanf()`. What is the contents of the file? – MikeCAT Mar 03 '16 at 03:00
  • Also see [Why is `while ( !feof(file) )` always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – lurker Mar 03 '16 at 03:05
  • @AndySchweig ok i delete it, and it seems fine, because it has already comparison on it. and yeah i stupidly does some calculation and prints out inside the while loop :D, it works, it prints like this : `Total Hit =448264 Total Miss =551736 Hitt Rate =44.83% Miss Rate =55.17% Total Reference =1000000` – mas bro Mar 03 '16 at 03:18
  • @MikeCAT i appreciate it your help too :D thank you very much! – mas bro Mar 03 '16 at 03:19

1 Answers1

1

It already has a comparison on it, and yeah, it stupidly does some calculation and prints out inside the while loop. It works, it prints like this:

Total Hit =448264 Total Miss =551736 Hitt Rate =44.83% Miss Rate =55.17% Total Reference =1000000

Tom Aranda
  • 5,919
  • 11
  • 35
  • 51
mas bro
  • 312
  • 1
  • 12