首页 > 解决方案 > 如何从C中的文件中读取数据并根据数据执行程序

问题描述

我目前有这个我正在工作的程序。当我从键盘输入值时它可以工作,但现在我试图通过从文件中读取数据并使其根据文件中的数据执行。这是文件中的数据...

1
65536
1027
16
1
65536
1024
4096
1
65536
1024
16
3
65535
14
2
65535
3
65534
512
2
1023
4

这是我扫描文件并存储输入的地方。

//Declare and initialize variables.
int  option = 0;
int  mainMemSize = 65536;
int  cacheSize = 1024;
int  blockSize = 16;
int  tags = mainMemSize / cacheSize;
int  *mainMemPtr = NULL;
line *cachePtr = NULL;
FILE *filePtr;

//Initialize the memory.
mainMemPtr = initMainMemory(mainMemSize);
cachePtr = initCache(cacheSize);

filePtr = fopen("prog2_test_data.txt", "r");

printf("*** Starting to read data from file: prog2_data_test.txt");
fscanf(filePtr, "%d", &option);

do
{
    showMenu();

    switch (option)
    {
        case 1:
            freeCache(&cachePtr, tags);
            free(mainMemPtr);
            setParameters(mainMemSize, cacheSize, blockSize);
            tags = mainMemSize / cacheSize;
            mainMemPtr = initMainMemory(mainMemSize);
            cachePtr = initCache(cacheSize);
            break;

        case 2:
            readCache(mainMemPtr, cachePtr, mainMemSize, blockSize, cacheSize);
            break;

        case 3:
            writeCache(mainMemPtr, cachePtr, mainMemSize, blockSize, cacheSize);
            break;

        case 4:
            break;
    }

}while (option != 4);

if (cachePtr != NULL)
    freeCache(&cachePtr, tags);

if (mainMemPtr != NULL)
    free(mainMemPtr);

fclose(filePtr);
printf("***Memory Freed Up - Program Terminated");
getchar();
}
void setParameters(int mainMemSize, int cacheSize, int blockSize)
{
if (blockSize > cacheSize) 
{
    printf("*** Error – Block size is larger than cache size\n");
}
else if ((cacheSize % blockSize) != 0) 
{
    printf("*** Error – Cache size is not a power of 2\n");
}
else if ((blockSize % 2 == 0) && (cacheSize % 2 == 0)) 
{
    printf("***Data Accepted \n");
}
}

void showMenu()
{
printf("\nMain memory to Cache memory mapping: \n");
printf("------------------------------------ \n");
printf("1) Enter Configuration Parameters\n");
printf("2) Read from cache\n");
printf("3) Write to cache\n");
printf("4) End\n");
}

int* initMainMemory(int size)
{

int j;

//initialize main memory.
int* ptr = (int*)malloc(size * sizeof(int));

for (j = 0; j < size; ++j)
    *(ptr + j) = size - j;

return ptr;
}

line* initCache(int tags)
{
int j;
line* ptr = (line*)malloc(tags * sizeof(line));


for (j = 0; j < tags; ++j)
{
    ptr[j].tag = -1;
    ptr[j].block = NULL;
}

return ptr;
} 

void freeCache(line **ptr, int size)
{
int j = 0;

for (; j < size; ++j)
{
    if ((*ptr)[j].block != NULL)
        free((*ptr)[j].block);
}
free(*ptr);
}

void readCache(int* mainMemPtr, line* cachePtr, int mmSize, int blockSize, 
int cacheSize)
{

//Declare and initialize variables.
int address = 0;
int value = 0;
int tag;
int myBlock;
int word;
int j;
int baseOffset;
int alreadyMissed = 0;

address = mmSize;

//Compute.
baseOffset = (address / blockSize) * blockSize;
tag = address / cacheSize;
word = address % blockSize;
myBlock = (address % cacheSize) / blockSize;

//Check if tag does not match or not.
if (cachePtr[myBlock].tag != tag)
{

    //Display this.
    printf("***Cache hit\n");
    alreadyMissed = 1;
    cachePtr[myBlock].tag = tag;
}

//Check if cache block memory is equal to NULL or not.
if (cachePtr[myBlock].block == NULL)
{

    //Condition check.
    if (alreadyMissed == 0)
        printf("***Cache hit\n");

    //Block Allocation.
    cachePtr[myBlock].block = (int*)malloc(blockSize * sizeof(int));
}

//Read from the main memory
for (j = 0; j < blockSize; ++j)
{
    cachePtr[myBlock].block[j] = mainMemPtr[baseOffset + j];
}

printf("Word %d of block %d with tag %d have %d\n", word, myBlock, tag, 
cachePtr[myBlock].block[word]);
}

void writeCache(int* mainMemPtr, line* cachePtr, int mmSize, int 
blockSize, int cacheSize)
{

//Declare and initialize variables.
int address = 0;
int value = 0;
int tag;
int myBlock;
int word;
int j;
int baseOffset;
int alreadyMissed = 0;


address = mmSize;

//Compute.
baseOffset = (address / blockSize) * blockSize;
tag = address / cacheSize;
word = address % blockSize;
myBlock = (address % cacheSize) / blockSize;

//Assign new value.
mainMemPtr[address] = value;

//Check if tag does not match or not.
if (cachePtr[myBlock].tag != tag)
{
    printf("***Write miss - First Load block from memory\n");
    alreadyMissed = 1;
    cachePtr[myBlock].tag = tag;
}

//Check if cache block memory is equal to NULL or not.
if (cachePtr[myBlock].block == NULL)
{
    if (alreadyMissed == 0)
        printf("Write miss!\n");

    //Block Allocation.
    cachePtr[myBlock].block = (int*)malloc(blockSize * sizeof(int));
}

//Transfer from the main memory to the cache.
for (j = 0; j < blockSize; ++j)
    cachePtr[myBlock].block[j] = mainMemPtr[baseOffset + j];


printf("***Word %d of block %d with tag %d have %d\n", word, myBlock, tag, 
cachePtr[myBlock].block[word]);
}

这应该是程序开始调试后的期望输出。

*** 开始从输入文件中读取数据:prg2_data.txt


*** 错误 - 缓存大小不是 2 的幂!返回主菜单


*** 错误 - 块大小大于缓存大小!返回主菜单


*** 接受所有输入参数。开始处理写/读请求


*写小姐...首先从内存中加载块!*带有标记 63 的高速缓存行 63 的字 15 包含值 14


* 高速缓存命中 *带有标记 63 的高速缓存行 63 的字 15 包含值 14


* 高速缓存命中 *带有标记 63 的高速缓存行 63 的字 14 包含值 512


*读小姐...从内存中加载块!*带有标记 0 的缓存行 63 的字 15 包含值 64513


*** 内存释放 - 程序正常终止

相反,发生的是一个无限循环。我似乎无法发现问题。有人可以帮助我吗?

标签: c

解决方案


while (!eof(filePtr))
    fscanf(filePtr, "%d %d %d %d", &option, &mainMemSize, &cacheSize, &blockSize);

这里有两点不对:

  1. eof函数不预测未来。您不能使用它来预测未来的读取将失败,从而避免该读取。相反,检查读取是否实际成功或失败,如果失败,则停止。

  2. 这会一遍又一遍地重复fscanf操作,每次都会覆盖以前的结果。你不想fscanf在它返回后再次调用,所以循环不应该在这里。


推荐阅读