簡體   English   中英

如何從C中的文件中讀取數據並使程序根據數據執行

[英]How to read data from a file in C and make the program execute according to the data

我目前有我正在使用的這個程序。 當我從鍵盤輸入值時它起作用,但現在我試圖通過從文件中讀取數據並使其根據文件中的數據執行來使其工作。 這是文件中的數據...

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


*** 內存已釋放 - 程序正常終止

相反,發生的是一個無限循環。 我似乎無法發現問題。有人可以幫助我。

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

這里有兩件事不對:

  1. eof函數不能預測未來。 你不能用它來預測未來的讀取會失敗,從而避免讀取。 相反,檢查讀取實際上是成功還是失敗,如果失敗,則停止。

  2. 這會一遍又一遍地重復fscanf操作,每次都會覆蓋之前的結果。 你不想在fscanf返回后再次調用它,所以循環不應該在這里。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM