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 :

langage C et la structure pile


Sujet :

C

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Profil pro
    Étudiant
    Inscrit en
    Février 2010
    Messages
    32
    Détails du profil
    Informations personnelles :
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2010
    Messages : 32
    Par défaut langage C et la structure pile
    salut tout le monde,
    mon but c'est de séparer les champs d'une ligne et les stocker dans une pile, ces champs dans la ligne (sous forme de chaine de caractères) sont séparés par des ","
    par exemple la ligne est:
    125895,A,8807,USER,TRD,1,TLX,
    pour cela j'ai fait cette fonction:

    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
    static pile* Sep_Line (char * line_in)
    {
     
    printf("Beginning of Sep_Line() for the line: %s \n", line_in);
     
       char* line_aux = malloc(sizeof(char) * MAX_LINE_LEN);
       printf("allocation for the line \n");
       char *field 	;
       field = malloc(sizeof(char) * MAX_FIELD_NAME_LEN);
     
       printf("allocation for pile \n");
       pile *ligne = malloc(sizeof(pile));
       printf("the allocation of pile is done \n");
     
       if (ligne == NULL)
       {
        printf("allocation failed \n ");
       }
     
       int i=0;
       int l;
       size_t len;
     
       strcpy(line_aux, line_in);
       printf("the line on which we are separating fields is : %s \n",line_aux); 
       strcat(line_aux, ",");
       printf("the line is now : %s \n", line_aux);
       strcat(line_aux,"\0");
       l= strlen(line_aux);
     
    /* Storing the line's content in a "pile" */
     
    do
    {
        len = strcspn(line_aux, ","); /* len contain the lenght of the initial segment of line_aux which doed not contain "," */
        printf("the length of the fields is :%d \n",len);
        strncpy (field, line_aux, len); /* field will contain the 1st field in the 1st iteration the second one in the second iteration ainsi de suite */
        strcat (field,"\0"); 
        printf("the field's value is : %s \n", field);
     
        Push(&ligne, field); /* In the first iteartion, the "pile" will contain the first field */
        printf("we stored the field's value in the pile \n");
        line_aux = line_aux + len + 2; /* To make line_aux points to the next field: line_aux will point to the caracter just following the "," */
     
    } 
     
    while (line_aux == "\0");
     
    printf("End of Sep_Line() \n \n");
     
    return (ligne);
     
    }
    la structure pile que j'utilise est:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    typedef struct pile {
       char* content;
       struct pile * prev;
      } pile;
    et la fonction Push est:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    void Push(pile **p, char *Val)
     
    {
            pile *element= malloc(sizeof(pile));
            if (element == NULL)  /* If the allocation is failed. */
                    {
                    exit(EXIT_FAILURE);
                    }
            element->content = Val;
            element->prev = *p;
            *p = element;       /* The pointer points to the last element */
    }
    lorsque j'exècute la fonction Sep_Line , il s'arrête au message:
    the field's value is : BRDG_TAG
    et il m'affiche : Segmentation Fault
    je comprends pas où est la Segmentation Fault!!??
    SVP quelqu'un pourrait m'aider !! c'est urgent
    et merci

  2. #2
    Membre éprouvé
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mars 2011
    Messages
    59
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2011
    Messages : 59
    Par défaut
    Salut,

    à première vue, les entrées dans la pile vont collisionner, tu prends tjrs le même pointeur qui sera écrasé par la suite, je pense qu'il faut un
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
     element->content = strdup(Val)
    dans la fonction Push

    ensuite j'ai un doute sur le while(line_aux == "\0") à moins que tes données d'entrée aient un formatage spécial

    Cdt

  3. #3
    Membre averti
    Profil pro
    Étudiant
    Inscrit en
    Février 2010
    Messages
    32
    Détails du profil
    Informations personnelles :
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2010
    Messages : 32
    Par défaut
    bonjour ,
    même avec le strdup j'ai toujours la segmentation fault

  4. #4
    Expert confirmé
    Avatar de diogene
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Juin 2005
    Messages
    5 761
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2005
    Messages : 5 761
    Par défaut
    Beaucoup d'erreurs :
    Push() : déjà signalée
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
            element->content = strdup(Val); //     element->content = Val;
    Sep_Line() :
    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
    ....
    // printf("allocation for pile \n");
       pile *ligne = NULL;    // malloc(sizeof(pile));
    // printf("the allocation of pile is done \n");
    // if (ligne == NULL)
    // {
    //    printf("allocation failed \n ");
    // }*/
    //  int i=0; 
    //  int l;
    .....
         printf("the line is now : %s \n", line_aux);
     //  strcat(line_aux,"\0"); inutile déjà mis par le strcat() précédent
     //  l= strlen(line_aux);   inutile
    .....
       strncpy (field, line_aux, len); /* field will contain the 1st field in the 1st iteration the second one in the second iteration ainsi de suite */
       field[len] = '\0'; // strcat (field,"\0");  faux : strncpy ne met pas (toujours) un 0 terminal :
                          // strcat inutilisable
    .....
      line_aux = line_aux + len + 1; // pas +2
    .....
    while (line_aux[0] != '\0'); //while (line_aux == "\0");

  5. #5
    Membre averti
    Profil pro
    Étudiant
    Inscrit en
    Février 2010
    Messages
    32
    Détails du profil
    Informations personnelles :
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2010
    Messages : 32
    Par défaut
    J'ai fais les modifications dont vous m'avez signalez
    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
    static pile* Sep_Line (char * line_in)
    {
     
    printf("Beginning of Sep_Line() for the line: %s \n", line_in);
    trace("Beginning of Sep_Line() \n");
     
       char* line_aux = malloc(sizeof(char) * MAX_LINE_LEN);
       char *field 	;
       field = malloc(sizeof(char) * MAX_FIELD_NAME_LEN);
       pile *ligne = NULL;
     
       int i=0;
       int L;
       size_t len;
     
       strcpy(line_aux, line_in);
       strcat(line_aux, ",");
       strcat(line_aux,"\0");
       printf("the line is now : %s \n", line_aux);
     
    /* Storing the line's content in a "pile" */
     
    do
    {
        len = strcspn(line_aux, ","); /* len contains the lenght of the initial segment of line_aux which doed not contain "," */
        printf("the field's length is :%d \n", len);
        strncpy (field, line_aux, len); /* field will contain the 1st field in the 1st iteration the second one in the second iteration ainsi de suite */
        field[len]='\0';
        printf("the field's value is : %s \n", field);
     
        Push(&ligne, field); /* In the first iteartion, the "pile" will contain the first field */
        printf("we stored the field's value in the pile \n");
        line_aux = line_aux + len + 1; /* To make line_aux points to the next field: line_aux will point to the caracter just following the "," */
        printf("the rest from the line is :%s \n",line_aux);
        View(ligne);
    } // fin de do
    while (line_aux[0] != '\0'); 
    printf("End of Sep_Line() \n \n");
    return (ligne);
    }
    et l'exécution me donne cela:

    Beginning of Sep_Line() for the line: BRDG_TAG,ACTION_IND,BORG_CODE,BORG_XCOD_CODE,BFUN_CODE,ADDR_NUM,CONTACT_TYPE,CONTACT_CODE
    the line is now : BRDG_TAG,ACTION_IND,BORG_CODE,BORG_XCOD_CODE,BFUN_CODE,ADDR_NUM,CONTACT_TYPE,CONTACT_CODE ,
    the field's length is :8
    the field's value is : BRDG_TAG
    we stored the field's value in the pile
    the rest from the line is :ACTION_IND,BORG_CODE,BORG_XCOD_CODE,BFUN_CODE,ADDR_NUM,CONTACT_TYPE,CONTACT_CODE ,
    BRDG_TAG
    the field's length is :10
    the field's value is : ACTION_IND
    Segmentation Fault


    always a segmentation fault
    whhhhhhhyyyyyyy!!!!!

  6. #6
    Modérateur
    Avatar de Obsidian
    Homme Profil pro
    Chercheur d'emploi
    Inscrit en
    Septembre 2007
    Messages
    7 478
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Chercheur d'emploi
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2007
    Messages : 7 478
    Par défaut
    Citation Envoyé par ensi_meriem Voir le message
    always a segmentation fault
    whhhhhhhyyyyyyy!!!!!
    Est-ce que tu sais ce qu'est une segmentation fault, déjà ? Si non, ça te ferait gagner beaucoup de temps de commencer par cela …

Discussions similaires

  1. pile langage c
    Par baylamat dans le forum C
    Réponses: 3
    Dernier message: 27/12/2006, 15h28
  2. Structure du langage et fonctions
    Par $@&_# dans le forum Langage
    Réponses: 1
    Dernier message: 26/11/2006, 20h11
  3. [langage] Titiller la pile ?
    Par kij dans le forum Langage
    Réponses: 7
    Dernier message: 26/04/2005, 19h12
  4. [langage] [traduction] Structure SWITCH
    Par rbh dans le forum Langage
    Réponses: 6
    Dernier message: 07/06/2004, 17h24
  5. [langage] structures complexes et affichage
    Par mat21 dans le forum Langage
    Réponses: 5
    Dernier message: 18/02/2004, 15h38

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