Pb d'allocation mémoire malloc

Résolu
oz80 Messages postés 4 Date d'inscription mercredi 16 novembre 2005 Statut Membre Dernière intervention 2 décembre 2007 - 18 nov. 2005 à 14:32
cs_Matt67 Messages postés 549 Date d'inscription samedi 6 septembre 2003 Statut Membre Dernière intervention 6 mars 2010 - 25 nov. 2005 à 21:51
Bonjour à tous,

j'ai un problème d'allocation de mémoire je pense.
Je déclare une variable comme ceci :


Code:
,
----

char* Retour = NULL;
Retour = (char*)malloc(((Chemin_fichier_renomme.longueur_)+5)* sizeof(char));

Puis je fais une boucle pour remplir Retour :


Code:
,
----

for(k=0; k<Chemin_fichier_renomme.longueur_; k++)
{
Retour[k] = Chemin_fichier_renomme.tableau_[k];
}

Puis :


Code:
,
----

Retour[k] = '.';
Retour[k+1] = 'h';
Retour[k+2] = 't';
Retour[k+3] = 'm';
Retour[k+4] = 'l';

Mais quand je regarde ce qu'il y a dans Retour il m'affiche bien le chemin du fichier "F:\.....\fichier.html" mais il me rajoute des caractères derrière.

Pouvez-vous m'aider please ?

Merci d'avance

2 réponses

ymca2003 Messages postés 2070 Date d'inscription mardi 22 avril 2003 Statut Membre Dernière intervention 3 juillet 2006 7
18 nov. 2005 à 15:05
Il faut rajouter le nul de fin à la chaîne :
for(k=0; k<Chemin_fichier_renomme.longueur_; k++)
{
Retour[k] = Chemin_fichier_renomme.tableau_[k];
}
Retour[k] = '\0';
3
cs_Matt67 Messages postés 549 Date d'inscription samedi 6 septembre 2003 Statut Membre Dernière intervention 6 mars 2010 3
25 nov. 2005 à 21:51
Bonsoir,



char *retour = NULL;

size_t taille = chemin_fichier.longueur + 6;

retour = (char*)malloc(taille * sizeof(char));

memset(retour, 0, taille);

memcpy(retour, chemin_fichier.tableau, chemin_fichier.longueur);

strcat(retour, ".html")


Matt...
0
Rejoignez-nous