So I have another lab project which is to write a cache simulator in C. First I need to read the address in HEX and then I can do the process. My idea is to read every single line and store the whole line into array. For example, I want to store 0x00A0E00E in to Array[0].
The text file looks something like this:
0x00000033
0x00000003
0x00000003
0x00000003
0x00000003
0x0000005A
0x0000000B
0x00000000
0x00000009
0x0000000A
My original code was trying to read every single character in the file, which made it harder to do the operations. Here is my code:
/*
*This is the program for lab assignment3 the Cache Simulator
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define BLOCK_SIZE 4 // each block store 4 words
#define FILE_NAME "address.txt"
//function prototypes
void readFile(FILE *filePtr, char* inFileName);
float hitRatio(int totalAccessMem, int totalHit);
float hitRatio(int totalAccessMem, int totalHit)
{
float hitRatio = 0.0;
hitRatio = totalHit / totalAccessMem;
return hitRatio;
}
void readFile(FILE *filePtr, char* inFileName)
{
char address[5500];
//open the file
filePtr = fopen(inFileName , "r");
int i=0;
for(i; i<5500; i++)
{
fscanf(filePtr, "%c", &address[i]);
printf("%c", address[i]);
}
//printing test
fclose(filePtr);
}
int main()
{
int totalAccessMem = 0; // default value of total access memory
int totalHit = 0; // default value of total hits
char inFileName[] = FILE_NAME;
FILE *ptr;
readFile(ptr, inFileName);
return 0;
}
Also, is that possible for me to modify the member inside each array member? For example, is that possible for me to edit every single letter if the member in Array[0] is 0x00000033 ?
User contributions licensed under CC BY-SA 3.0