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 :

Fichier : filtrage


Sujet :

C

  1. #1
    Membre confirmé
    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
    Points : 460
    Points
    460
    Par défaut Fichier : filtrage
    Bonjour,

    Je souhaite pour un fichier donné pourvoir l'afficher dans la console dans un premier temps en appliquant un filtrage. Par exemple ne pas afficher les lignes contenant le caractère '-'.

    J'arrive à récupérer les lignes contenant ce caractère, cependant la partie du code qui s'occupe d'appliquer le filtre ne fais rien. En fait il ne rentre pas dans la boucle. J'ai mis des printf("1"); au début et à la fin pour m'en assurer et en effet rien ne s'affiche. A priori, je pense que s'il ne rentre pas c'est parce qu'il n'arrive pas à ouvrir le fichier puisque pour rentrer dans ma boucle while, je fais un fscanf sur le fichier en question.
    Après, peut être que le code à l'intérieur du while ne se déroule pas correctement mais je n'arrive même pas à y rentrer.

    Voici la boucle en question :
    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
        // ON AFFICHE LE FICHIER EN SUPPRIMANT LES LIGNES CONTENANT LE CARACTERE '-'
        fopen("noms.txt"   ,"r");
        char C;
        int position_caractere=0;
        int ligne=0;
     
        // VARIABLE POUR CONNAITRE LES POSITIONS DES CARACTERES A RECOPIER
        int position_debut_ligne=0;
        int position_fin_ligne;
     
        // ON COMMENCE A PARCOURIR LE FICHIER
        while( fscanf(F1,"%c",&C) != EOF )
             {
                 // POSITION COURANTE DANS LE FICHIER
                 position_caractere++;
     
                 // SI LE CARACTERE COURANT VAUT LE CARACETERE DE CONTROL DE FIN DE LIGNE
                 if ( C == '\n' )
                    {
                        // ON INCREMENTE LA VALEUR DE LIGNE
                        ligne++;
                        // ON PRECISE QUE LE DERNIER CARACTERE A RECOPIER EST CELUI DE LA FIN DE LA LIGNE
                        position_fin_ligne=position_caractere;
                    }
     
                 // ON VERIFIE QUE LA LIGNE N'APPARTIENT PAS AU TABLEAU DES LIGNES A SUPPRIMER
                 int i;
                 int BOOL=1;
                 for ( i=0 ; i < (*TAILLE_TAB_NOM) ; i++ )
                     {
                           // SI LA LIGNE COURANTE N'APPARTIENT PAS AU TABLEAU DES LIGNES A SUPPRIMER
                           if ( TAB_OCCURENCE_NOM[i] != ligne && BOOL == 1 )
                              {
                                  // LA LIGNE COURANTE POURRA ETRE COPIEE
                                  BOOL=1;
                              }
     
                           // SINON ON NE LA RECOPIERA PAS
                         else BOOL==0;
                         printf("\n%d",BOOL);
                     }
     
                 // ON TEST BOOL POUR SAVOIR SI ON PEUT FINALEMENT COPIER LA LIGNE
                 if ( BOOL == 1 )
                    {
                           // ON CREE UNE NOUVELLE POSITION CARACTERE
                           fopen("noms.txt"   ,"r");
                           int temp_position_caractere=0;
                           while( fscanf(F1,"%c",&C) != EOF )
                                {
                                    temp_position_caractere++;
                                    // SI LA POSITION DU CARACTERE EST COMPRIS ENTRE LE DEBUT ET LA FIN DE LA LIGNE, ON AFFICHE
                                    if ( position_debut_ligne <= temp_position_caractere && temp_position_caractere <= position_fin_ligne )
                                       {
                                            printf("%c", C);
                                       } 
                                }
                           position_debut_ligne=position_fin_ligne+1;
                           fclose(F1);
                    }                    
             }  
     
             fclose(F1);
    Voici le code dans son intégralité :
    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
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    #include<stdio.h>
    #include<stdlib.h>
    #include <windows.h>
     
    int         LIGNE_FICHIER( FILE *FICHIER                                                   );
    int        TAILLE_FICHIER( FILE *FICHIER                                                   );
    int     OCCURENCE_FICHIER( FILE *FICHIER, char CARACTERE                                   );
    void    MEMORISER_FICHIER( FILE *FICHIER, char *CHAINE                                     );
    void OCCURENCE_DANS_LIGNE( FILE *FICHIER, int **TAB_OCCURENCE, int *TAILLE, char CARACTERE );
     
    int main()
    {
        // OBTENIR LE NOMBRE DE LIGNES
        FILE *F0=fopen("prenoms.txt","r");
        FILE *F1=fopen("noms.txt"   ,"r+w");
        int ligne_nom    = LIGNE_FICHIER( F0 );
        int ligne_prenom = LIGNE_FICHIER( F1 );
        fclose(F1);
        fclose(F0);
     
        // OBTENIR LE NOMBRE DE CARACTERES
        fopen("prenoms.txt","r");
        fopen("noms.txt"   ,"r");
        int taille_nom    = TAILLE_FICHIER(F1);
        int taille_prenom = TAILLE_FICHIER(F0);
        fclose(F1);
        fclose(F0);
     
        // OCCURENCE D'UN CARACTERE
        fopen("prenoms.txt","r");
        fopen("noms.txt"   ,"r");
        int occurence_nom    = OCCURENCE_FICHIER(F1, '-');
        int occurence_prenom = OCCURENCE_FICHIER(F0, 'Z');
        fclose(F1);
        fclose(F0);
     
        // PLACER FICHIER EN MEMOIRE
        char *NOM    = malloc(   taille_nom*sizeof(char));
        char *PRENOM = malloc(taille_prenom*sizeof(char));
     
        fopen("prenoms.txt","r");
        fopen("noms.txt"   ,"r");
        MEMORISER_FICHIER( F0, PRENOM );
        MEMORISER_FICHIER( F1, NOM    );
        fclose(F1);
        fclose(F0);
     
        // OCCURENCE DANS LIGNE
        int *TAILLE_TAB_NOM       = malloc(sizeof(int));
        int *TAILLE_TAB_PRENOM    = malloc(sizeof(int));
        int *TAB_OCCURENCE_NOM    = NULL;
        int *TAB_OCCURENCE_PRENOM = NULL;
     
        fopen("prenoms.txt","r");
        fopen("noms.txt"   ,"r");
        OCCURENCE_DANS_LIGNE( F0, &TAB_OCCURENCE_PRENOM, TAILLE_TAB_PRENOM, 'Z' );
        OCCURENCE_DANS_LIGNE( F1, &TAB_OCCURENCE_NOM   , TAILLE_TAB_NOM   , '-' );
        fclose(F1);
        fclose(F0);
     
        // ON AFFICHE LES RESULTATS OBTENUS
        printf("NOMS    : %d TAILLE : %d OCCURENCE : %d \nPRENOMS : %d TAILLE : %d OCCURENCE : %d",ligne_nom, taille_nom, occurence_nom, ligne_prenom, taille_prenom, occurence_prenom);
     
        // ON AFFICHE LE FICHIER EN SUPPRIMANT LES LIGNES CONTENANT LE CARACTERE '-'
        fopen("noms.txt"   ,"r");
        char C;
        int position_caractere=0;
        int ligne=0;
     
        // VARIABLE POUR CONNAITRE LES POSITIONS DES CARACTERES A RECOPIER
        int position_debut_ligne=0;
        int position_fin_ligne;
     
        // ON COMMENCE A PARCOURIR LE FICHIER
        while( fscanf(F1,"%c",&C) != EOF )
             {
                 // POSITION COURANTE DANS LE FICHIER
                 position_caractere++;
     
                 // SI LE CARACTERE COURANT VAUT LE CARACETERE DE CONTROL DE FIN DE LIGNE
                 if ( C == '\n' )
                    {
                        // ON INCREMENTE LA VALEUR DE LIGNE
                        ligne++;
                        // ON PRECISE QUE LE DERNIER CARACTERE A RECOPIER EST CELUI DE LA FIN DE LA LIGNE
                        position_fin_ligne=position_caractere;
                    }
     
                 // ON VERIFIE QUE LA LIGNE N'APPARTIENT PAS AU TABLEAU DES LIGNES A SUPPRIMER
                 int i;
                 int BOOL=1;
                 for ( i=0 ; i < (*TAILLE_TAB_NOM) ; i++ )
                     {
                           // SI LA LIGNE COURANTE N'APPARTIENT PAS AU TABLEAU DES LIGNES A SUPPRIMER
                           if ( TAB_OCCURENCE_NOM[i] != ligne && BOOL == 1 )
                              {
                                  // LA LIGNE COURANTE POURRA ETRE COPIEE
                                  BOOL=1;
                              }
     
                           // SINON ON NE LA RECOPIERA PAS
                         else BOOL==0;
                         printf("\n%d",BOOL);
                     }
     
                 // ON TEST BOOL POUR SAVOIR SI ON PEUT FINALEMENT COPIER LA LIGNE
                 if ( BOOL == 1 )
                    {
                           // ON CREE UNE NOUVELLE POSITION CARACTERE
                           fopen("noms.txt"   ,"r");
                           int temp_position_caractere=0;
                           while( fscanf(F1,"%c",&C) != EOF )
                                {
                                    temp_position_caractere++;
                                    // SI LA POSITION DU CARACTERE EST COMPRIS ENTRE LE DEBUT ET LA FIN DE LA LIGNE, ON AFFICHE
                                    if ( position_debut_ligne <= temp_position_caractere && temp_position_caractere <= position_fin_ligne )
                                       {
                                            printf("%c", C);
                                       } 
                                }
                           position_debut_ligne=position_fin_ligne+1;
                           fclose(F1);
                    }                    
             }  
     
             fclose(F1);     
     
             getchar();
    }
     
    int LIGNE_FICHIER( FILE *FICHIER )
    {
        int  NOMBRE_LIGNES = 0;
        char C                ;
     
        if( FICHIER != NULL )
          {
                   while( fscanf(FICHIER,"%c",&C) != EOF )
                        {
                            if (  C == '\n' )
                               {
                                   NOMBRE_LIGNES++;
                               }                    
                        }
     
                   return NOMBRE_LIGNES;
          }
     
      else printf("\n ERREUR DANS LA FONCTION LIGNE_FICHIER\n   + VERIFIEZ QUE LE FICHIER EXISTE");
    }
     
    int TAILLE_FICHIER( FILE *FICHIER )
    {
        int  TAILLE = 0;
        char C                ;
     
        if( FICHIER != NULL )
          {
                   while( fscanf(FICHIER,"%c",&C) != EOF )
                        {
                                   TAILLE++;                   
                        }
     
                   return TAILLE;
          }
     
      else printf("\n ERREUR DANS LA FONCTION TAILLE_FICHIER\n   + VERIFIEZ QUE LE FICHIER EXISTE");
    }
     
    int OCCURENCE_FICHIER( FILE *FICHIER, char CARACTERE )
    {
        int  OCCURENCES = 0;
        char C            ;
     
        if( FICHIER != NULL )
          {
                   while( fscanf(FICHIER,"%c",&C) != EOF )
                        {
                            if (  C == CARACTERE )
                               {
                                   OCCURENCES++;
                               }                    
                        }
     
                   return OCCURENCES;
          }
     
      else printf("\n ERREUR DANS LA FONCTION LIGNE_FICHIER\n   + VERIFIEZ QUE LE FICHIER EXISTE");
    }
     
    void MEMORISER_FICHIER( FILE *FICHIER, char *CHAINE )
    {
        if( FICHIER != NULL && CHAINE != NULL )
          {
                   int  i = 0;
                   char C    ;
     
                   while( fscanf(FICHIER,"%c",&C) != EOF )
                        {
                            CHAINE[i]=C;
                            i++;               
                        }
          }
     
      else printf("\n ERREUR DANS LA FONCTION MEMORISER_FICHIER\n   + VERIFIEZ QUE LE FICHIER EXISTE\n   +VERIFIEZ QUE LA CHAINE EST CORRECTEMENT REFERENCEE");
    }
     
    void OCCURENCE_DANS_LIGNE( FILE *FICHIER, int **TAB_OCCURENCE, int *TAILLE, char CARACTERE )
    {
        char  C                ;
        int   i             = 0;
        int   LIGNE         = 0;
     
        *TAILLE        = 1                      ;
        *TAB_OCCURENCE = malloc( 1*sizeof(int) );
     
        if( FICHIER != NULL && TAB_OCCURENCE !=NULL && TAILLE != NULL )
          {
                   while( fscanf(FICHIER,"%c",&C) != EOF )
                        {
                            if ( C == CARACTERE )
                               {
                                   (*TAB_OCCURENCE)[i]=LIGNE;
                                   i++;                                          
     
                                   if ( i !=0 )
                                      {
                                            *TAILLE=*TAILLE+1;
                                            realloc(*TAB_OCCURENCE, ((*TAILLE)*sizeof(int)));
                                            (*TAB_OCCURENCE)[i]=LIGNE;
                                      }                 
                               }
     
                             if ( C == '\n' )
                               {
                                   LIGNE++;
                               }                    
                        }
     
                        *TAILLE=*TAILLE-1;
          }
     
      else printf("\n ERREUR DANS LA FONCTION OCCURENCE_DANS_LIGNE\n   + VERIFIEZ QUE LE FICHIER EXISTE\n   + VERIFIEZ SI VOS POINTEURS SONT CORRECTEMENTS REFERENCES");
    }
    Merci d'avance !
    UNE REPONSE UTILE : &|| UN PROBLEME RESOLU :

  2. #2
    Membre émérite Avatar de SofEvans
    Homme Profil pro
    Développeur C
    Inscrit en
    Mars 2009
    Messages
    1 076
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France

    Informations professionnelles :
    Activité : Développeur C

    Informations forums :
    Inscription : Mars 2009
    Messages : 1 076
    Points : 2 328
    Points
    2 328
    Par défaut
    fopen retourne un type FILE*.
    Si tu pense que fopen n'ouvre pas le fichier, il faudrai le verifier.

    Du style

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    FILE* F1 = fopen("noms.txt"   ,"r");
    if (F1 == NULL)
    {
        perror("Erreur lors de l'ouverture du fichier noms.txt ");
    }
    else
    {
        /* La c'est bon */
    }
    Sinon, je n'ai pas regarder tout le code.
    Si l'erreur persiste, regarde a la fac comment on detecte correctement la fin de fichier (je ne sais plus comment on fait exactement).

  3. #3
    Rédacteur
    Avatar de Franck.H
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2004
    Messages
    6 951
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : France, Haut Rhin (Alsace)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Service public

    Informations forums :
    Inscription : Janvier 2004
    Messages : 6 951
    Points : 12 462
    Points
    12 462
    Par défaut
    Je n'ai pas parcouru tout le code mais je pense que pour la lecture du fichier c'est inutilement compliqué, une lecture ligne par ligne avec fgets puis une recherche du caractère '-' avec strchr suffit
    Mon Site
    Ma bibliothèque de gestion des chaînes de caractères en C

    L'imagination est plus importante que le savoir. A. Einstein

    Je ne répond à aucune question technique par MP, merci d'avance !

  4. #4
    Membre confirmé
    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
    Points : 460
    Points
    460
    Par défaut
    Ok je ne connaissais pas ces fonctions. Je verrais ce que je peux faire avec.
    Je voulais avant tout coder au moins une fois ce genre d'opération histoire de savoir ce qui s'y passe.
    UNE REPONSE UTILE : &|| UN PROBLEME RESOLU :

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

Discussions similaires

  1. Filtrage dans un fichier Trace
    Par scorpion-ca dans le forum MATLAB
    Réponses: 1
    Dernier message: 05/12/2007, 09h55
  2. Filtrage de fichiers
    Par Harpoon dans le forum VBA Access
    Réponses: 2
    Dernier message: 26/10/2007, 13h56
  3. [FileFilter] filtrage de fichiers
    Par Premium dans le forum Entrée/Sortie
    Réponses: 5
    Dernier message: 05/07/2007, 20h43
  4. Filtrage de fichiers
    Par money mark dans le forum Shell et commandes GNU
    Réponses: 1
    Dernier message: 22/03/2006, 19h15
  5. [ant] copie de fichier + filtrage
    Par c.tranquille dans le forum ANT
    Réponses: 4
    Dernier message: 10/10/2005, 11h51

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