如何从C中的文件读取数据并使程序根据数据执行

时间:2018-10-28 03:37:17

标签: 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


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

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

1 个答案:

答案 0 :(得分:0)

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

这里有两件事是错误的:

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

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