IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C Discussion :

problème de fichiers


Sujet :

C

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre habitué
    Femme Profil pro
    Inscrit en
    Février 2013
    Messages
    9
    Détails du profil
    Informations personnelles :
    Sexe : Femme

    Informations forums :
    Inscription : Février 2013
    Messages : 9
    Par défaut problème de fichiers
    Bonjour à tous , j'essaie de prendre le contenu d'un fichier dans un tableau à 2 dimensions de telle sorte que chaque 76 caractères du fichier représentent une ligne du tableau mais ca blocke est ce que vous pouvez m'aider voici mon essai
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    #include <stdio.h>
    #include <stdlib.h>
     
    int main(int argc, char *argv[])
    { FILE* fichier=NULL;
      int carte[28][76]={0};
      int i=0,j=0;
      char ligne[76*28+1]={0};
      fichier=fopen("test.txt","r");
      fgets(ligne,76*28+1,fichier);
      for (i=0; i<28; i++){
        for (j=0; j<76; j++){
                           switch(ligne[i*5+j])
                            {        case '0':
                                     carte[i][j]=0;
                                     case '1':
                                     carte[i][j]=1;
                                     case '2':
                                     carte[i][j]=2;
                                     case '3':
                                     carte[i][j]=3;
                                     case '4':
                                     carte[i][j]=4;
                                     case '5':
                                     carte[i][j]=5;
                                     case '6':
                                     carte[i][j]=6;
                                     case '7':
                                     carte[i][j]=7;
     
                            }
    }
      }
      getch();
      for (i=0; i<28; i++){
        for (j=0; j<76; j++){
            printf("%d",carte[i][j]);
     
        }
    }
     
      system("PAUSE");
      return 0;
    }
    et voici le contenu du fichier test.txt:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    701010101000070202020202020000070303030303030000000050505050505050505050500010101010100000020202020200000000030303030303030000000050505050505050505000001010101010000000020202000000000000030303030303030000000050505050505050000000101010101000000000020000000000000000030303030303030000000050505050500000000010101010100000000000000000000040000000000000000000000000000050505000000000000000000000000000400000000000004040000000000000000000000000000070000000000000000000000000004040400000000000404040000000000000000000000000000000000000000000000000000040404040400000000040404040000000000000000000000000000000000000000000000000404040404040400000004040404040000000000000000000000000000000000000000000000000004040404040000000404040404040000000000000000000000000000000000000000000000000404040404000000040404040404040000000000000000000000000000000000000000000000040404040400000004040404040404040000000000000000000000000000000000000000000004040404040000000404040404040404040000000000000000000000000000000000000000000404040404000000040404040404040404040000000000000000000000000000000000000000040404040404040404040404040404040404000000000000000000000000000000000000000000040404040404040404040404040404040000000000000000000000000000000000000000000000040404040404040404040404040400000000000000000000000000000000000000000000000000040404040404040404040404000000000000000000000000000000000000000000000000000004040404040404040404040000000000000000000000000000000000000000000000000000000004040404040404040400000000000000000000000000000000000006000000000000000000000004040404040404040000000000000000000000000000000000000606000000000000000000000004040404040404040000000000000000000000000000000000060606000000000000000000000404040404040404040000000000000000070000000000000006060606000000000000000004040404040404040404040000000000000606060000000000000606060606000000000000040404040404040404040404040000000006060606060000000000060606060606000000000404040404040404040404040404040000060606060606060000000006060606060606000000000000000000020000000000000000000606060606060606060000000606060606060606000000000000000202020000000000000006060606060606060606060000060606060606060606000000000002020202020000000000060606060606060606060606060007060606060606060606000000020202020202070000000606060606060606060606060606060
    merci d'avance

  2. #2
    Membre éclairé
    Homme Profil pro
    amateur
    Inscrit en
    Octobre 2007
    Messages
    731
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : amateur

    Informations forums :
    Inscription : Octobre 2007
    Messages : 731
    Par défaut
    Salut,

    Je ne connais pas ton niveau en C, de ce fait, je ne sais pas si le code que je vais te proposer t'évoquera des choses.

    De ce que je vois, ton fichier fait 2280 charactères soit pour 72 colonnes, un équivalent de 30 lignes et non 29.

    Je n'ai pas tellement commenter le code alors si tu as des questions, surtout n'hésites pas.

    Voici le résultat après exécution de ce code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
     
    70101010100007020202020202000007030303030303000000005050505050505050505
    10101010100000020202020200000000030303030303030000000050505050505050505
    10101010100000000202020000000000000303030303030300000000505050505050500
    10101010100000000002000000000000000003030303030303000000005050505050000
    10101010100000000000000000000040000000000000000000000000000050505000000
    00000000000000004000000000000040400000000000000000000000000000700000000
    00000000000000404040000000000040404000000000000000000000000000000000000
    00000000000040404040400000000040404040000000000000000000000000000000000
    00000000004040404040404000000040404040400000000000000000000000000000000
    00000000000000404040404000000040404040404000000000000000000000000000000
    00000000000000404040404000000040404040404040000000000000000000000000000
    00000000000000404040404000000040404040404040400000000000000000000000000
    00000000000000404040404000000040404040404040404000000000000000000000000
    00000000000000404040404000000040404040404040404040000000000000000000000
    00000000000000404040404040404040404040404040404040000000000000000000000
    00000000000000004040404040404040404040404040404000000000000000000000000
    00000000000000000040404040404040404040404040400000000000000000000000000
    00000000000000000000404040404040404040404040000000000000000000000000000
    00000000000000000000404040404040404040404000000000000000000000000000000
    00000000000000000000004040404040404040400000000000000000000000000000000
    60000000000000000000000040404040404040400000000000000000000000000000000
    60600000000000000000000000404040404040404000000000000000000000000000000
    60606000000000000000000000404040404040404040000000000000000070000000000
    60606060000000000000000040404040404040404040400000000000006060600000000
    60606060600000000000004040404040404040404040404000000000606060606000000
    60606060606000000000404040404040404040404040404040000060606060606060000
    60606060606060000000000000000000200000000000000000006060606060606060600
    60606060606060600000000000000020202000000000000000606060606060606060606
    60606060606060606000000000002020202020000000000060606060606060606060606
    70606060606060606060000000202020202020700000006060606060606060606060606

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <string.h>
     
    char *allocate_1D_c( unsigned long long int n );
    int *allocate_1D_i( unsigned long long int n );
    void deallocate_1D_i( int *p );
    void deallocate_1D_c( char *p );
    void deallocate_2D_i( int **p, unsigned long long int x );
    int **allocate_2D_i( unsigned long long int x, unsigned long long int y );
    unsigned long long int str_length(const char *S);
    void file_close(FILE *f);
    FILE *file_open(const char *p, const char *r);
    unsigned long long int file_length( const char *p );
    char *file_to_string( const char *p );
    int matrix_get_m( unsigned long long int element_qty, unsigned long long int n);
    int char_to_int( char c );
    void matrix_fill( char *s, int **matrix, unsigned long long int m, unsigned long long int n );
    void matrix_display( int **matrix, unsigned long long int m, unsigned long long int n );
     
    int main()
    {
        // Redimensionnement de la console en 150*100
        system("mode con cols=150 lines=100");
     
        // Le fichier text.txt est copié dans un tableau de char
        char *s = file_to_string("text.txt");
     
        // Calcul de la longueur du fichier texte maintenant copié dans le tableau de char s
        unsigned long long int s_length = str_length(s);
     
        // Ton fichier fait s_length caractères et ta matrice est définie comme MAT(m,76).
        // Ici on calcule m
        unsigned long long int m = matrix_get_m(s_length, 76);
     
        // Maintenant que l'on connait la taille, on peut allouer une matrice de m*76 éléments
        int **matrix = allocate_2D_i(m,76);
     
        // A partir de la chaine s, on remplit cette matrice
        matrix_fill(s, matrix, m, 76);
     
        // Une fois remplie, on peut l'afficher
        matrix_display(matrix, m, 72);
     
        // Libération de la mémoire alloué.
        deallocate_1D_c(s);
        deallocate_2D_i(matrix, m);
     
        return 0;
    }
     
    char *allocate_1D_c( unsigned long long int n )
    {
        unsigned long long int i;
        char *p = NULL;
        if ((p = (char *)malloc(n*sizeof(char))))
            for ( i=0 ; i<n ; i++ )
                p[i]='\0';
     
        else printf("allocate.h::allocate_1D_c -> %s\n", strerror(errno));
     
        return p;
    }
     
    int *allocate_1D_i( unsigned long long int n )
    {
        unsigned long long int i;
        int *p = NULL;
        if ((p = (int *)malloc(n*sizeof(int))))
            for ( i=0 ; i<n ; i++ )
                p[i]=0;
     
        else printf("allocate.h::allocate_1D_i -> %s\n", strerror(errno));
     
        return p;
    }
     
    void deallocate_1D_i( int *p )
    {
        if (p)
            free(p), p=NULL;
     
        else printf("deallocate.h::deallocate_1D_i -> %s\n",strerror(errno));
    }
     
    void deallocate_1D_c( char *p )
    {
        if (p)
            free(p), p=NULL;
     
        else printf("deallocate.h::deallocate_1D_c -> %s\n",strerror(errno));
    }
     
    void deallocate_2D_i( int **p, unsigned long long int x )
    {
        if (p)
        {
            unsigned long long int i;
            for ( i=0 ; i<x ; i++ )
                free(p[i]), p[i]=NULL;
     
            free(p), p=NULL;
        }
        else printf("deallocate.h::deallocate_2D_i -> %s\n",strerror(errno));
    }
     
    int **allocate_2D_i( unsigned long long int x, unsigned long long int y )
    {
        int **t = NULL;
        if ( (t = (int**)malloc( x*sizeof(int*))) )
        {
            unsigned long long int i;
            for ( i=0 ; i<x ; i++ )
            {
                if ( !( t[i] = allocate_1D_i(y) ) )
                {
                    printf("allocate.h::allocate_2D_i -> %s\n", strerror(errno));
                    deallocate_2D_i(t,i-1);
                    i=x;
                }
            }
        }
        else printf("allocate.h::allocate_2D_i -> %s\n", strerror(errno));
        return t;
    }
     
    unsigned long long int str_length(const char *S)
    {
        unsigned long long int c = 0;
        while( *S != '\0')
            c++, S++;
     
        return c;
    }
     
    void file_close(FILE *f)
    {
        fclose(f);
    }
     
    FILE *file_open(const char *p, const char *r)
    {
        FILE *f = NULL;
        if ( !(f=fopen(p,r)) )
            printf("file.h::file_open -> %s\n", strerror(errno));
     
        return f;
    }
     
    unsigned long long int file_length( const char *p )
    {
        char c;
        unsigned long long int l = 0;
        FILE *f = file_open(p,"r");
        while( fscanf(f,"%c",&c) != EOF )
            l++;
     
        file_close(f);
        return l;
    }
     
    char *file_to_string( const char *p )
    {
        char c;
        unsigned long long int i = 0;
        char *s = allocate_1D_c( file_length(p)+1 );
        FILE *f = file_open(p,"r");
        while( fscanf(f,"%c",&c) != EOF )
        {
            s[i] = c;
            i++;
        }
        file_close(f);
        return s;
    }
     
    int matrix_get_m( unsigned long long int element_qty, unsigned long long int n)
    {
       return element_qty/n;
    }
     
    int char_to_int( char c )
    {
        if ( c == '0' ) { return 0; }
        if ( c == '1' ) { return 1; }
        if ( c == '2' ) { return 2; }
        if ( c == '3' ) { return 3; }
        if ( c == '4' ) { return 4; }
        if ( c == '5' ) { return 5; }
        if ( c == '6' ) { return 6; }
        if ( c == '7' ) { return 7; }
        if ( c == '8' ) { return 8; }
        if ( c == '9' ) { return 9; }
     
        return -1;
    }
     
    void matrix_fill( char *s, int **matrix, unsigned long long int m, unsigned long long int n )
    {
        unsigned long long int i,j;
        for ( i=0 ; i<m ; i++ )
            for (j=0; j<n ; j++)
                matrix[i][j] = char_to_int( s[j + n*i] );
    }
     
    void matrix_display( int **matrix, unsigned long long int m, unsigned long long int n )
    {
        printf(" ");
        unsigned long long int i,j;
        for ( i=0 ; i<m ; i++ )
            for (j=0; j<n ; j++)
            {
                if ( j==n-1 )
                    printf("\n%c", matrix[i][j]);
     
                else printf("%d",matrix[i][j]);
            }
    }
    Bon courage !

  3. #3
    Membre habitué
    Femme Profil pro
    Inscrit en
    Février 2013
    Messages
    9
    Détails du profil
    Informations personnelles :
    Sexe : Femme

    Informations forums :
    Inscription : Février 2013
    Messages : 9
    Par défaut
    Merci pour votre aide ,mais le problème est résolue ,j'ai fait quelque erreurs et je l'ai corrigé

  4. #4
    Membre éclairé
    Homme Profil pro
    amateur
    Inscrit en
    Octobre 2007
    Messages
    731
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : amateur

    Informations forums :
    Inscription : Octobre 2007
    Messages : 731
    Par défaut
    Avec une matrice de 28*76, j'en doute.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Delphi 3] Problème avec fichier ressource .RES
    Par delphim dans le forum EDI
    Réponses: 2
    Dernier message: 28/06/2005, 18h11
  2. [Servlet] Probléme écriture fichier
    Par hedgehog dans le forum Servlets/JSP
    Réponses: 4
    Dernier message: 23/05/2005, 15h17
  3. Problème lecture fichier en C++
    Par cali1983 dans le forum C++
    Réponses: 17
    Dernier message: 20/05/2005, 09h36
  4. Problème ouverture fichier par double clic...
    Par sankookai dans le forum MFC
    Réponses: 4
    Dernier message: 11/05/2005, 09h13
  5. Problème avec fichier texte
    Par jpdelx dans le forum ASP
    Réponses: 7
    Dernier message: 13/11/2003, 13h17

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo