Julian
Julian

Reputation: 1675

C - Reading a file and writing the contents to an array one char at a time

I'm trying to learn about emulation while re-learning C while I'm at it (I learned it in college, but haven't done much with it lately). I'm writing a chip8 emulator, but I'm having issues loading the game into the emulator's memory. The problem appears to me that I'm not using fopen() correctly, and the file stream I'm trying to create either isn't finding the file, or I'm just doing something incorrect in my implementation.

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);
}

I put a comment in all capitals on the line where my logical error occurs, so it would stand out.

This is my main method, for reference. And my I have a rom of pong in the same directory as this compiled code.

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

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

    loadGame("PONG");
}

I appreciate any insight anyone may have! Let me know if I should add or be more descriptive anywhere.

Edit:

I think I got it using MrZebra's information! I wanted to post my answer which I believe is working as I want. This is my new loadGame() method.

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++;
    }
}

Upvotes: 0

Views: 238

Answers (2)

ryyker
ryyker

Reputation: 23208

If

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

results in game == NULL, it likely means that fopen() could not find "PONG".

The argument for your line:

loadGame("PONG");  

Should be a path, similar for example to:

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

Upvotes: 0

Zebra North
Zebra North

Reputation: 11482

It looks like a simple "file not found" or "access denied". Double check that the executable is really where you think it is - depending on your build environment it might be under a \Debug subdirectory or something similar.

Also I'm guessing your data is a binary file - fgets() is for reading text, you want fread(), and you should open the file in binary mode "rb".

Upvotes: 1

Related Questions