Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reading integer from file through function in c returns wrong value

I’m trying to read a bunch of information about a player from a binary file in c through the following code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


// structure joueur
typedef struct Joueur {
    char nom[20];
    int num_lieu;
    int liste_objet[10];
}Joueur;


// question a
void sauvegarder_jeu(char* nom_partie, Joueur* joueur) {
    // ouverture du fichier
    FILE *flot;
    flot = fopen(nom_partie, "wb");
    if (flot == NULL) {
        printf("Erreur dans l'ouverture du fichier!\n");
        return;
    }

    // écriture du nom du joueur
    fwrite(joueur->nom, sizeof(joueur->nom), 1, flot);
    // écriture du numero du lieu
    fwrite(&(joueur->num_lieu), sizeof(joueur->num_lieu), 1, flot);
    // écriture de la liste des objets
    fwrite(joueur->liste_objet, sizeof(joueur->liste_objet), 1, flot);

    // fermeture du fichier
    fclose(flot);
}

// question b
void charger_jeu(char* nom_partie, char* nom, int* num_lieu, int* liste_objet) {
    // ouverture du fichier
    FILE *flot;
    flot = fopen(nom_partie, "rb");
    if (flot == NULL) {
        printf("Erreur dans l'ouverture du fichier!\n");
        return;
    }

    // joueur temp pour sizeof
    Joueur *temp = (Joueur*)malloc(sizeof(Joueur));

    // lecture du nom du joueur
    fread(nom, sizeof(temp->nom), 1, flot);
    // écriture du numero du lieu
    fread(&num_lieu, sizeof(temp->num_lieu), 1, flot);
    // écriture de la liste des objets
    fread(liste_objet, sizeof(temp->liste_objet), 1, flot);

    // suppression du joueur temporaire
    free(temp);

    // fermeture du fichier
    fclose(flot);
}


int main() {
    // variables
    char *nom_partie = "save.sve";
    int i;
    int* num_lieu_lecture;
    int* liste_objet_lecture;
    char* nom_lecture;

    // creation d'un joueur qui possede tous les objets
    Joueur *j1 = (Joueur*)malloc(sizeof(Joueur));
    strcpy(j1->nom, "Omar");
    j1->num_lieu = 12;
    for (i = 0; i < 10; i++) {
        j1->liste_objet[i] = 1;
    }

    // sauvegarde de la partie
    sauvegarder_jeu(nom_partie, j1);
    printf("Sauvegarde terminee!\n");

    // lecture de la partie
    charger_jeu(nom_partie, nom_lecture, num_lieu_lecture, liste_objet_lecture);
    printf("Chargement terminee!\n");

    // affichage des donnees de la partie
    printf("%s\n", nom_lecture);
    printf("%d\n", *num_lieu_lecture);
    for (i = 0; i < 10; i++) {
        printf("liste_objet[%d] = %d\n", i, liste_objet_lecture[i]);
    }

    // liberation de la memoire
    free(j1);

    return 0;
}

The function "sauvegarder_jeu()" writes the player’s data into a binary file, and the "charger_jeu()" is supposed to read that data and store into variables that I would print out.

The output, where all the values are correct expect for the "num_lieu_lecture" value:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Omar
32759
liste_objet[0] = 1
liste_objet[1] = 1
liste_objet[2] = 1
liste_objet[3] = 1
liste_objet[4] = 1
liste_objet[5] = 1
liste_objet[6] = 1
liste_objet[7] = 1
liste_objet[8] = 1
liste_objet[9] = 1

I don’t know where the problem stems from.

>Solution :

The variables you’re passing to charger_jeu() do not need to be pointers. num_liste_objet should be int, and liste_objet_lecture and nom_lecture should be arrays.

Then you should pass the address of num_liste_objet to charger_jeu(), so it will update the variable. In charger_jeu() you don’t need to use & before num_lieu because it’s already a pointer.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


// structure joueur
typedef struct Joueur {
    char nom[20];
    int num_lieu;
    int liste_objet[10];
}Joueur;


// question a
void sauvegarder_jeu(char* nom_partie, Joueur* joueur) {
    // ouverture du fichier
    FILE *flot;
    flot = fopen(nom_partie, "wb");
    if (flot == NULL) {
        printf("Erreur dans l'ouverture du fichier!\n");
        return;
    }

    // écriture du nom du joueur
    fwrite(joueur->nom, sizeof(joueur->nom), 1, flot);
    // écriture du numero du lieu
    fwrite(&(joueur->num_lieu), sizeof(joueur->num_lieu), 1, flot);
    // écriture de la liste des objets
    fwrite(joueur->liste_objet, sizeof(joueur->liste_objet), 1, flot);

    // fermeture du fichier
    fclose(flot);
}

// question b
void charger_jeu(char* nom_partie, char* nom, int* num_lieu, int* liste_objet) {
    // ouverture du fichier
    FILE *flot;
    flot = fopen(nom_partie, "rb");
    if (flot == NULL) {
        printf("Erreur dans l'ouverture du fichier!\n");
        return;
    }

    // joueur temp pour sizeof
    Joueur *temp = (Joueur*)malloc(sizeof(Joueur));

    // lecture du nom du joueur
    fread(nom, sizeof(temp->nom), 1, flot);
    // écriture du numero du lieu
    fread(num_lieu, sizeof(temp->num_lieu), 1, flot);
    // écriture de la liste des objets
    fread(liste_objet, sizeof(temp->liste_objet), 1, flot);

    // suppression du joueur temporaire
    free(temp);

    // fermeture du fichier
    fclose(flot);
}


int main() {
    // variables
    char *nom_partie = "save.sve";
    int i;
    int num_lieu_lecture;
    int liste_objet_lecture[10];
    char nom_lecture[20];

    // creation d'un joueur qui possede tous les objets
    Joueur *j1 = (Joueur*)malloc(sizeof(Joueur));
    strcpy(j1->nom, "Omar");
    j1->num_lieu = 12;
    for (i = 0; i < 10; i++) {
        j1->liste_objet[i] = 1;
    }

    // sauvegarde de la partie
    sauvegarder_jeu(nom_partie, j1);
    printf("Sauvegarde terminee!\n");

    // lecture de la partie
    charger_jeu(nom_partie, nom_lecture, &num_lieu_lecture, liste_objet_lecture);
    printf("Chargement terminee!\n");

    // affichage des donnees de la partie
    printf("%s\n", nom_lecture);
    printf("%d\n", *num_lieu_lecture);
    for (i = 0; i < 10; i++) {
        printf("liste_objet[%d] = %d\n", i, liste_objet_lecture[i]);
    }

    // liberation de la memoire
    free(j1);

    return 0;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading