• [^] # Re: merci...

    Posté par (site web personnel) . En réponse au message concatenation de chaine. Évalué à 2.

    Regarde mon commentaire plus haut :
    vc_tmp = (char *) malloc (sizeof(vc_car));
    A mettre a la place :
    vc_tmp = (char *) malloc (2*sizeof(char));
    
    Ton code deviendrais :
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main (void)
    {
    	FILE *output;
    	char vc_car;
    	char *vc_str = NULL;
    	// Execution de la commande OS
    	output = popen ("echo sortie_commande", "r");
    	if (!output)
    	{
    		perror("erreur popen");
    		return EXIT_FAILURE;
    	}
    	vc_car = getc(output);
    	while (vc_car != EOF)
    	{
    		if (!vc_str)
    		{
    			// premiere allocation de memoire pour vc_str
    			vc_str = (char *) malloc (2*sizeof(char));
    			if (!vc_str)
    			{
    				perror("erreur malloc");
    				return EXIT_FAILURE;
    			}
    		}
    		else
    		{
    			// on augmente la taille de vc_str d'un caractère
    			vc_str = (char *)realloc (vc_str, strlen(vc_str)+2*sizeof(char));
    			if (!vc_str)
    			{
    				perror("erreur realloc");
    				return EXIT_FAILURE;
    			}
    		}
    		vc_str[strlen(vc_str)] = vc_car;
    		vc_str[strlen(vc_str)] = '0円';
    		// Affichage
    		printf("%c\n", vc_car);
    		printf("x%sx\n", vc_str);
    		printf("--------------\n");
    		// Nouveau caractère
    		vc_car = getc(output);
    	}
    	if (pclose (output) != 0)
    	{
    		perror("erreur pclose");
    		return EXIT_FAILURE;
    	}
    	return EXIT_SUCCESS;
    }