c - 将结构写入二进制文件,从文件中读取,存储在缓冲区中,打印缓冲区

标签 c file binaryfiles

我正在尝试将结构写入二进制文件,标点符号将作为 insertInFile 函数中的参数接收,(使用 raspberryPi,并使用开关存储时间 react 值)。然后我想从文件中读取并将值存储在堆上,最后读取我动态存储的那些值。我遇到了问题,因为它无法正确读取或无法写入二进制文件。

typedef enum dificulty {
    EASY, MEDIUM, HARD
} Dificulty;
typedef struct player {
    char nickname[MAXSIZE];
    enum dificulty Dificulty;
    float pontuacion;
} Player;

#define LED_CYCLES 5
#define MAX_PLAYERS 2

int insertInFile(float const *const timeReceived, unsigned short *level, char *playerName) {
    Player players[MAX_PLAYERS];
    int count = 0, i;
    FILE *fplayers;

    //check if file can be opened
    if ((fplayers = fopen("game.bin", "wb")) == NULL) {
        fputs("Erro ao abrir ficheiro\n", stderror);
        return (-1);
    }
    //go to the beginning of the file
    rewind(fplayers);

    //cycle that allows to save int the bin file the struct "Player"
    for (i = 0; i < count; i++) {
        Player[i].pontuacion = &timeReceived[count];
        Player[i].Dificulty = &level;
        Player[i].nickname = &playerName;
        fwrite(&players, sizeof (Player), count, fplayers);
    }

    //close the bin file
    fclose(fplayers);

    return 0;
}

void obtainFromFile() {
    Player players;
    int count = 0;
    FILE *fplayers;
    size_t size;
    unsigned char *buffer;
    int i;

    //open file
    fp = fopen("game.bin", "rb");
    fseek(fp, 0, SEEK_END);
    size = ftell(fp);        // calculate the size needed
    fseek(fp, 0, SEEK_SET);
    buffer = (unsigned char *)malloc(size);

    if (fplayers == NULL) {  // check some error if file == empty
        printf("Error\n", stderr);
        return (-1);
    } else
    if (fread(&buffer, sizeof (*buffer), size, fp) != size) // if count of read bytes != calculated size of .bin file -> ERROR
        printf("Error\n", stderr);
        return (-1);
    } else {
        for (i = 0; i < size; i++) {
            printf("%02x", buffer[i]);
        }
    }
    fclose(fp);
    free(buffer);
}

最佳答案

insertInFile()中存在对结构使用的误解。编写Player.pontuacion是不对的。

To assign a value in an array of struct Player players[MAX_PLAYERS];, use players[i].Dificulty = (Dificulty)level.

//cicle that allows to save int the bin file the struct "Player"
for (i = 0; i < count; i++) {
    players[i].pontuacion = timeReceived[count];
    players[i].Dificulty = (Dificulty)(level[count]);
    strncpy(players[i].nickname,playerName,MAXSIZE-1);
    players[i].nickname[MAXSIZE]='\0';
}
// write count players
fwrite(&(players[0]), sizeof (Player), count, fplayers);

而不是:

for (i = 0; i < count; i++) {
    Player.pontuacion = &timeReceived[count];
    Player.Dificulty = &level;
    Player.nickname = &playerName;
    fwrite(&players, sizeof (Player), count, fplayers);
}

关于c - 将结构写入二进制文件,从文件中读取,存储在缓冲区中,打印缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41014552/

相关文章:

c - 如何在范围除法留余数后获得相等数量的元素

c - 专业阵列处理

python - 如何使用 Python 3 将文本写入以二进制模式打开的文件中?

mysql - 优化wordpress mysql数据库文件排序415885行

java - 如何隐藏 Android 中现有的文件夹?

github - 在 GitHub 上分发我的项目的二进制文件的最佳方式是什么?

excel - 有没有一种好方法可以跟踪 GIT 存储库中 Excel 工作表的变化?

c - 如何更新 C 中显示的时间?

c - 在 C 中格式化字符串

java - 如何将文件从一个 URL 复制到另一个 URL 而不存储在本地计算机上