• # merci...

    Posté par . En réponse au message concatenation de chaine. Évalué à 1.

    Merci beaucoup à vous deux, vos réponses m'ont vraiment dépanné. J'ai réussi à faire fonctionner mon programme mais il y a quelque chose qui m'étonne un peu : quand je fais le realloc, je prend uniquement strlen(vc_str) + 1 et pourtant ça ne plante pas. J'avais pourtant lu qu'il fallait toujours réserver un caractère en fin de chaine pour le caractere null (qui indique la fin de la chaine). Le nouveau code source:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int
    main (void)
    {
     FILE *output;
     char vc_car;
     char *vc_tmp = NULL;
     char *vc_str = NULL;
     // Execution de la commande OS
     output = popen ("echo sortie_commande", "r");
     if (!output) {
     fprintf (stderr, "incorrect parameters or too many files.\n");
     return EXIT_FAILURE;
     }
     vc_tmp = (char *) malloc (sizeof(vc_car));
     vc_car = getc(output);
     while (vc_car != EOF) {
     if (vc_str == NULL) {
     // premiere allocation de memoire pour vc_str
     vc_str = (char *) malloc (sizeof(vc_car));
     if (vc_str == NULL) {
     printf("Cannot allocate memory.\n");
     }
     }
     else {
     // on augmente la taille de vc_str au fur et a mesure des caracteres lus
     vc_str = realloc (vc_str, strlen(vc_str) + 1);
     if (vc_str == NULL) {
     printf("Cannot allocate memory.\n");
     }
     }
     *vc_tmp = vc_car;
     vc_str = strncat(vc_str, vc_tmp, 1);
     // Affichage
     fprintf (stdout, "vc_car : %c\n", vc_car);
     fprintf (stdout, "vc_str : x%sx\n", vc_str);
     fprintf (stdout, "--------------\n");
     vc_car = getc(output);
     }
     if (pclose (output) != 0)
     {
     fprintf (stderr, "Could not run more or other error.\n");
     }
     return EXIT_SUCCESS;
    }