2014-04-26 94 views
0

我試圖在學習C的同時重新學習C,而我在它的時候(我在大學學到了它,但沒有最近用它做了很多)。我正在寫一個chip8仿真器,但我在將遊戲加載到仿真器的內存時遇到了問題。問題在我看來,我沒有正確使用fopen(),並且我正在嘗試創建的文件流沒有找到該文件,或者我只是在執行中做了不正確的事情。C - 讀取文件並將內容寫入數組一個字符一次char

void loadGame(char* name) { 
    //Use fopen to read the program into memory, this loop will use the stream 
    FILE* game = fopen(name, "r"); //Open the game file as a stream. 
    unsigned int maxGameSize = 3584; //The max game size available 0x200 - 0xFFF 
    char gameBuffer[maxGameSize]; //The buffer that the game will be temporarily loaded into. 

    if (game == NULL) { //THIS IS WHERE THE ERROR HAPPENS. game does == NULL 
     fprintf(stderr, "The game either can't be opened or doesn't exist!\n"); 
     exit(1); 
    } else { 
     while (!feof(game)) { 
      if (fgets(gameBuffer, maxGameSize, game) == NULL) { //load the file into the buffer. 
       break; //Reached the EOF 
      } 
     } 

     //Now load the game into the memory. 
     int counter = 0; 
     while (counter < maxGameSize) { 
      memory[counter+512] = gameBuffer[counter]; 
      counter++; 
     } 
    } 

    fclose(game); 
} 

我在所有的首都發表了一個評論,我的邏輯錯誤發生了,所以它會脫穎而出。

這是我的主要方法,供參考。而且我的編譯代碼在同一個目錄下有一個pong。

int main(int argc, char **argv) { 

    setupGraphics(); //A stub for now 
    setupInput(); //Another stub for now. 
    initialize(); //Yet another stub. 

    loadGame("PONG"); 
} 

我很欣賞任何人可能有的見解!讓我知道是否應該在任何地方添加或更具描述性。

編輯:

我想我使用了MrZebra的信息!我想發佈我的答案,我相信這是我想要的。這是我的新的loadGame()方法。

void loadGame(char* name) { 
    unsigned int maxGameSize = 3584; //This is how many 8-bit cells is available in the memory. 
    char gameBuffer[maxGameSize]; //Temporary buffer to read game into from file. 

    FILE* game = fopen(name, "rb"); 

    if (game == NULL) { 
     perror("Failed to open game.\n"); 
    } else { 
     printf("Game found.\n"); 
    } 

    fread(gameBuffer, sizeof(char), maxGameSize, game); //Read file into temp buffer 
    fclose(game); 

    //Now load the game into the memory. 
    int counter = 0; 
    while (counter < maxGameSize) { 
     memory[counter+512] = gameBuffer[counter]; 
     counter++; 
    } 
} 
+0

確保您已閱讀了'name'文件權限和路徑'名稱「確實存在。你也可以使用'errno'(通過包含適當的頭文件)並打印'errno'來查看實際出錯的地方。 – mtahmed

+0

1)函數loadGame()裏的'name'是什麼意思? 2)不要使用'feof()'。只要測試你使用的任何閱讀功能的結果。 – chux

回答

1

它看起來像一個簡單的「文件未找到」或「拒絕訪問」。仔細檢查可執行文件是否真的是你認爲的可執行文件 - 取決於你的編譯環境,它可能位於\ Debug子目錄或類似的目錄下。

我也猜你的數據是一個二進制文件 - fgets()是閱讀文字,你想fread(),你應該在二進制模式"rb"打開該文件。

+0

查看fread文件(), size_t fread(void * ptr,size_t size,size_t nmemb,FILE * stream); 參數nmemb用於確定我指定的字節大小預期有多少個元素。這是否需要準確,或者我可以使用上限?我知道我可以使用feof檢查EOF –

+0

在你的if(game == null)塊中添加perror(「打開遊戲失敗」);這會給你錯誤信息,失敗。 – MrZebra

+0

抱歉,直到現在纔在評論中看到您的問題 - 是的項目數是上限,函數將返回讀取的項目數。 – MrZebra

0

如果

FILE* game = fopen(name, "r"); 

結果game == NULL,這可能意味着fopen()找不到 「PONG」。

你行的論點:

loadGame("PONG"); 

應該是一個路徑,例如類似:

"c:\\pongdev\\pong.exe" 
+0

是相對於二進制工作目錄的路徑嗎?我使用Linux,所以我可以做「./PONG」? –

+0

@JulianCleary - 抱歉,暫時未登錄。是的,路徑_can_可以是相對的,也可以是絕對路徑。你的選擇。 (現在你可能知道:) – ryyker

相關問題