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