Skip to content
Snippets Groups Projects
gestion_fichiers.c 3.17 KiB
/**
 * \file gestion_fichiers.c
 * \brief Code des fonctions travaillant sur les données de sauvagardes, de maps etc (fichier extérieurs)
 * \author Guillaume Vautrin
 * \version 1.0
 */

#include "gestion_fichiers.h"


char** allouer_tab_2D (int lig, int col){
    //Allocation d'un tableau de caractère à 2 dimensions
    char ** tab = malloc(lig * sizeof(char *)); 
    for(int i=0; i<lig; i++){
        tab[i] = malloc(col * sizeof(char));
    }

    return tab;
}

void desallouer_tab_2D (char** tab, int lig){
    //désallocation d'un tab de car 2D
    for (int i=0; i<lig; i++){
        free(tab[i]);
    }
    free(tab); 
}

void afficher_tab_2D (char** tab, int lig, int col){
    //Affiche le tableau 2D
    for (int i=0; i<lig; i++){
        for (int j=0; j<col; j++){
            printf("%c", tab[i][j]);
            if (j==col-1){
                printf("\n");
            }
        }
    }
}

void taille_fichier (const char* nomFichier, int* nbLig, int* nbCol){
    //Donne la taille d'un fichier texte (colonne et ligne)
    char test[3000]; //Stocke les caractères d'une ligne
    int ligne = 0; // nb de lignes
    int colonne =0; // nb de colonnes
    int temp = 0; // compte le nombre de caractère dans une ligne
    FILE* fichier = NULL;
    fichier = fopen(nomFichier, "r");
    if (fichier == NULL){
        printf("Erreur: fichier inaccessible \n");
    }else{
        while (fgets(test, sizeof test, fichier) != NULL){
            temp = strlen(test);
            if (temp > colonne){
                colonne = temp;
            }
            ligne++;
        }

        *nbCol = colonne-1;
        *nbLig = ligne;
    }
    fclose(fichier);
}

char** lire_fichier (const char* nomFichier){
    //Traduit le contenu d'un fichier dans un tableau de char
    FILE* fichier = fopen(nomFichier, "r");
    if (fichier == NULL){
        printf("Erreur: fichier inaccessible \n");
        fclose(fichier);
        return NULL;
    }
    //Création du tableau réceptacle
    int ligne = 0;
    int colonne = 0;
    taille_fichier(nomFichier, &ligne, &colonne);
    //printf("\n Taille ligne: %d | colonne : %d \n", ligne, colonne);
    char** tab = allouer_tab_2D(ligne, colonne);

    for (int i=0; i<ligne; i++){
        for (int j=0; j<colonne; j++){
            tab[i][j] = '\0';
        }
    }
    //Parcours du fichier
    char c = '\0'; //fgetc(fichier);        
    for (int i=0; i<=ligne; i++){
        for (int j=0; j<=colonne; j++){
            c = fgetc(fichier);
            if (c=='\n' || c== EOF) break;
            tab[i][j] = c;
        }
    }          
    fclose(fichier);
    return tab;

}

void genere_fichier (const char* nomFichier, char** source, int ligne, int colonne){
    //Génère un fichier a partir d'un tableau de caractère
    FILE* fichier = NULL;
    fichier = fopen(nomFichier, "w");
    if (fichier !=NULL){
        for (int i=0; i<ligne; i++){
            for (int j=0; j<colonne; j++){
                fputc(source[i][j], fichier);
                if (j==colonne-1){
                    fputc('\n', fichier);
                }
            }
        }
        fclose(fichier);
    }else{
        printf("\n Erreur: fichier n'a pas pu être ouvert ou généré \n");
    }
}