[C/C++] PROBLEME DE STR

sorakaan Messages postés 59 Date d'inscription   Statut Membre Dernière intervention   -  
Char Snipeur Messages postés 9813 Date d'inscription   Statut Contributeur Dernière intervention   -
Bonjour,

je creer un programme qui me pose probleme.
j'ai identifié la fonction rebelle et je l'ai isolée pour faire des tests.
je suis en stage et je suis contraint d'utiliser Visual studio pour coder en C,
ce que je trouve particulièrement long, chiant, et éprouvant pour les nerfs...
(y a qu'a voir la version windows du segfault pour comprendre -_-')

bref, voila pour commencer le code:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

//fonction qui pose probleme...
char	*g_cmd(char lect)
{
	char	*cmd;
    char    *res;
	int     i;

	cmd = "FFF%";
    res = NULL;
    res = (char*)malloc((strlen(cmd) + 1) * sizeof (*res));
	i = 0;
    while(cmd[i] != '\0')
	{
		if(cmd[i] == '%')
			res[i] = lect;
        else
            res[i] = cmd[i];
		i++;
	}
	return (res);
}

// main de test
int     main()
{
	char	lect;
    char    *cmd;

	lect = 68;
	system("@echo off");
	while (lect != 91)
	{
        cmd = g_cmd(lect);
		printf("%s",cmd);
        printf("%s","\n");
		lect = lect + 1;
	}
	system("echo termine");
	system("PAUSE");
	return (0);
}


je pense que ce code serait fonctionnel sous unix...
seulement voila, j'ai un probleme avec une chaine de caractere que je veux afficher...
car quand je lance le programme, il me renvoit ça:


do if exist 'D:' if not exist 'D:*' xcopy /F 'HARD_TEST*' 'D:GET_F' /e /q /y═²²²²½½½½½½½½■¯■¯■¯■
do if exist 'E:' if not exist 'E:*' xcopy /F 'HARD_TEST*' 'E:GET_F' /e /q /y═²²²²½½½½½½½½■¯■¯■¯■
do if exist 'F:' if not exist 'F:*' xcopy /F 'HARD_TEST*' 'F:GET_F' /e /q /y═²²²²½½½½½½½½■¯■¯■¯■
do if exist 'G:' if not exist 'G:*' xcopy /F 'HARD_TEST*' 'G:GET_F' /e /q /y═²²²²½½½½½½½½■¯■¯■¯■
do if exist 'H:' if not exist 'H:*' xcopy /F 'HARD_TEST*' 'H:GET_F' /e /q /y═²²²²½½½½½½½½■¯■¯■¯■
do if exist 'I:' if not exist 'I:*' xcopy /F 'HARD_TEST*' 'I:GET_F' /e /q /y═²²²²½½½½½½½½■¯■¯■¯■
do if exist 'J:' if not exist 'J:*' xcopy /F 'HARD_TEST*' 'J:GET_F' /e /q /y═²²²²½½½½½½½½■¯■¯■¯■
do if exist 'K:' if not exist 'K:*' xcopy /F 'HARD_TEST*' 'K:GET_F' /e /q /y═²²²²½½½½½½½½■¯■¯■¯■
do if exist 'L:' if not exist 'L:*' xcopy /F 'HARD_TEST*' 'L:GET_F' /e /q /y═²²²²½½½½½½½½■¯■¯■¯■
do if exist 'M:' if not exist 'M:*' xcopy /F 'HARD_TEST*' 'M:GET_F' /e /q /y═²²²²½½½½½½½½■¯■¯■¯■
do if exist 'N:' if not exist 'N:*' xcopy /F 'HARD_TEST*' 'N:GET_F' /e /q /y═²²²²½½½½½½½½■¯■¯■¯■
do if exist 'O:' if not exist 'O:*' xcopy /F 'HARD_TEST*' 'O:GET_F' /e /q /y═²²²²½½½½½½½½■¯■¯■¯■
do if exist 'P:' if not exist 'P:*' xcopy /F 'HARD_TEST*' 'P:GET_F' /e /q /y═²²²²½½½½½½½½■¯■¯■¯■
do if exist 'Q:' if not exist 'Q:*' xcopy /F 'HARD_TEST*' 'Q:GET_F' /e /q /y═²²²²½½½½½½½½■¯■¯■¯■
do if exist 'R:' if not exist 'R:*' xcopy /F 'HARD_TEST*' 'R:GET_F' /e /q /y═²²²²½½½½½½½½■¯■¯■¯■
do if exist 'S:' if not exist 'S:*' xcopy /F 'HARD_TEST*' 'S:GET_F' /e /q /y═²²²²½½½½½½½½■¯■¯■¯■
do if exist 'T:' if not exist 'T:*' xcopy /F 'HARD_TEST*' 'T:GET_F' /e /q /y═²²²²½½½½½½½½■¯■¯■¯■
do if exist 'U:' if not exist 'U:*' xcopy /F 'HARD_TEST*' 'U:GET_F' /e /q /y═²²²²½½½½½½½½■¯■¯■¯■
do if exist 'V:' if not exist 'V:*' xcopy /F 'HARD_TEST*' 'V:GET_F' /e /q /y═²²²²½½½½½½½½■¯■¯■¯■
do if exist 'W:' if not exist 'W:*' xcopy /F 'HARD_TEST*' 'W:GET_F' /e /q /y═²²²²½½½½½½½½■¯■¯■¯■
do if exist 'X:' if not exist 'X:*' xcopy /F 'HARD_TEST*' 'X:GET_F' /e /q /y═²²²²½½½½½½½½■¯■¯■¯■
do if exist 'Y:' if not exist 'Y:*' xcopy /F 'HARD_TEST*' 'Y:GET_F' /e /q /y═²²²²½½½½½½½½■¯■¯■¯■
do if exist 'Z:' if not exist 'Z:*' xcopy /F 'HARD_TEST*' 'Z:GET_F' /e /q /y═²²²²½½½½½½½½■¯■¯■¯■
termine
Appuyez sur une touche pour continuer...


comme vous pouvez le constater, l'affichage est un peu trop généreux à la fin de chaque ligne.
ce que j'aimerais savoir c'est:

- pourquoi ça m'ajoute ce "═²²²²½½½½½½½½■¯■¯■¯■" ???
- comment corriger ce problème...


merci d'avance pour vos réponses.

6 réponses

mastertnt
 
Il faut faire un memset sur res avant la boucle :

memset (res,'\0',strlen(cmd) + 1);


sinon tu peux remplacer aussi çà :

res=(char*)malloc((strlen(cmd) + 1) * sizeof (*res));

par

res=(char*)malloc((strlen(cmd) + 1) * sizeof (char));

c'est plus logique
1
Ronieee Messages postés 70 Date d'inscription   Statut Membre Dernière intervention   38
 
Ca fait lgtps que je n'ai pas fait de C, mais j'ai un sentiment bizarre quand tu reset le res a NULL :

char *cmd // Tu déclare une chaine de caractère par un pointeur alors que tu connais la taille de ta chaine

char *res; // Tu déclares une chaine de caractère (ou un pointeur vers un caratère)

cmd="FFF%" // Tu donnes une chaine de caractère fini a un pointeur non déclaré (tu ne l'as pas malloc)

res = NULL; // Tu mets ce pointeur a NULL et pas ce qu'il ya dans le pointeur (je ne sais plus si c grave)

res=(char*)malloc((strlen(cmd) + 1) * sizeof (*res));



Il ya plusieurs moyen de rendre ton code plus propre, en passant la chain de caractère res en paramètre plutot qu'en retour fonction (c'est un pointeur, c'est préférable)
0
Laurent
 
> je suis contraint d'utiliser Visual studio pour coder en C,
ce que je trouve particulièrement long, chiant, et éprouvant pour les nerfs...


Ceci n'a aucun sens.
Visual Studio, il n'y a pas mieux sur le marché pro pour faire du C ou C++ !!!
0
Marc
 
>system("echo termine");
>system("PAUSE");


C'est quoi ces horreurs ?
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
sorakaan Messages postés 59 Date d'inscription   Statut Membre Dernière intervention   2
 
merci pour vos réponses je verrai ça demain au taff car la j'ai plus accès au PC sur lequel je bosse.

0
Char Snipeur Messages postés 9813 Date d'inscription   Statut Contributeur Dernière intervention   1 299
 
Une autre solution est de faire res[i]='\0' juste après la boucle du while.
0