PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, November 12, 2022

[FIXED] Why fopen() does not open my data?

 November 12, 2022     c, memcached     No comments   

Issue

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);

    }
}

Solution

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



Answered By - mas bro
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing