| |||||
FORUM HardWare.fr

Programmation

[C]Insérer la valeur d'une variable au milieu d'une chaine de caracter| Dernière réponse | |
|---|---|
| Sujet : [C]Insérer la valeur d'une variable au milieu d'une chaine de caracter | |
| paranoidandroid | please... |
| Aperçu |
|---|
| Vue Rapide de la discussion |
|---|
| paranoidandroid | please... |
| paranoidandroid |
|
| paranoidandroid | Hummmmm ... |
| paranoidandroid | J'ai voulu improviser pour l'instertion de la chaine ...
Voila comment je m'y prends ... pouvez m'eexpliquer pkoi ça ne marche pas ... ? le .c: void read(char *fichier) { FILE *fic; char temp[32]; int i; fic = fopen("D:\\tables.txt","r" ); if(fic != NULL) { i=0; while(!feof(fic)) { fscanf(fic, "%s", temp); tables[i]=malloc(sizeof(temp)); strcpy(tables[i],temp); i++; } fclose(fic); } else printf("le fichier %s n'a pas pu etre ouvert",input_f); FILE *sortie; int j; char *cmd; sortie = fopen("greptables.bat","a" ); if(sortie != NULL) { for(j=0;j<i;j++) { cmd = insert(tables[j]); fputs(cmd, sortie); } fclose(sortie); free(tables); } } char *insert(char *table) { char *cmd_dbt; char *cmd_fin; char *cmd; char *cmd1; char *cmd2; cmd_dbt = "grep -i -l -c "; cmd_fin = " *.* >> "; cmd1=(strcat(cmd_dbt,table)); cmd2=(strcat(cmd1,cmd_fin)); cmd=(strcat(cmd2,"resultGrep.txt\n" )); return(cmd); } Comme vous pouvez le voir ( dumoins j'espere ) j'essaye de creer un fichier dans lequel chauqe ligne est une commande ou j'ai inséré une variable ( tables ). je voudrais obtenir : grep -i -l -c AAA *.* >> greptables.bat grep -i -l -c AAB *.* >> greptables.bat grep -i -l -c AAC *.* >> greptables.bat grep -i -l -c AAD *.* >> greptables.bat grep -i -l -c AEG *.* >> greptables.bat grep -i -l -c AFM *.* >> greptables.bat sachant que tables.txt est composé de : AAA AAB AAC AAD AEG AFM Qu'est ce qui ne va pas .... voilà ce que j'obtiens avec le code actuel : grep -i -l -c AAA *.* >> resultGrep.txt grep -i -l -c AAA *.* >> resultGrep.txt AAB *.* >> resultGrep.txt grep -i -l -c AAA *.* >> resultGrep.txt AAB *.* >> resultGrep.txt AAC *.* >> resultGrep.txt grep -i -l -c AAA *.* >> resultGrep.txt AAB *.* >> resultGrep.txt AAC *.* >> resultGrep.txt AAD *.* >> resultGrep.txt grep -i -l -c AAA *.* >> resultGrep.txt AAB *.* >> resultGrep.txt AAC *.* >> resultGrep.txt AAD *.* >> resultGrep.txt AEG *.* >> resultGrep.txt grep -i -l -c AAA *.* >> resultGrep.txt AAB *.* >> resultGrep.txt AAC *.* >> resultGrep.txt AAD *.* >> resultGrep.txt AEG *.* >> resultGrep.txt AFM *.* >> resultGrep.txt grep -i -l -c AAA *.* >> resultGrep.txt AAB *.* >> resultGrep.txt AAC *.* >> resultGrep.txt AAD *.* >> resultGrep.txt AEG *.* >> resultGrep.txt AFM *.* >> resultGrep.txt AFM *.* >> resultGrep.txt c un peu long je sais ... Merci |
| paranoidandroid | Merci de votre aide ;) |
| flo850 | pour mettre une var au milieu , je pense que tu as 2 possibilites:
- soit tu conais la longueur de ta phrase et pour recupere la vaueur tu fais : char *phrase; char *variable=malloc(size(char)*MAX); int lg_deb=strlen( debut de ta phrase ) int lg_fin=strlen( fin de ta phrase ) int lg = longueur totale .// phrase + variable strcpy(variable,phrase+lg_deb); variable[lg-lg_deb]='\0'; - soit tu ne la connais pas et tu devra fgaire un truc du style BLAbLA / variable / BLOBLO et utiliser la fonctoin strtok ( mais je me soiuviens plus trop comment elle marche ) resultat = |
| HelloWorld | char *variable[total]; for(i = 0; i < total; i++) { variable[i] = (char *)malloc(Taille_de_la_chaine * sizeof(char)); } le sizeof(char) c'est facultatif ici, mais est necessaire si tu as un tableau d'int par exemple (sizeof(int)) pour libérer c'est pareil, avec free(variable[i]); |
| paranoidandroid | Un exemple de l'utilisation de malloc ? |
| HelloWorld | Ben dans ce cas alors c'est normal aussi : tu lui donne l'adresse d'une variable où stocker la chaine (variable[i]) mais celle-ci n'existe pas (variable[i] est donc à NULL, d'où ton erreur)
T'as bien créé des pointeurs (char *variable[total];) mais ceux-ci ne pointent sur rien. Deux solutions : tu te compliques avec des malloc et des free pour leur affecter une variable, ou alors tu changes ainsi: char variable[total][Taille_max_d_une_chaine]; attention, c'est bien char variable et plus char * variable ! |
| paranoidandroid |
|
| HelloWorld | ben regarde la liste de parametres de fscanf ...
t'as tout inversé ! |
| paranoidandroid | C'est pour écrire chaque ligne dans un fichier ...
variable est une chaine de caracteres je veux dans mon fichier: "BLABLA BLA variable[1] BLOBLO" "BLABLA BLA variable[2] BLOBLO" "BLABLA BLA variable[3] BLOBLO" ... "BLABLA BLA variable[100] BLOBLO" *variable[total] est pour moi un tableau de chaine de caratere que je lirais dans un autre fichier ... ( Et je bloque en ce moment là dessus ) Lorsque je fais fscanf(variable[i], "%s", fic); j'ai une erreur quio n'apparait pas a la compil mais au debug ... ( variable = NULL ) [edtdd]--Message édité par paranoidandroid--[/edtdd] |
| HelloWorld |
|
| deathsharp | variable[i] c une phrase ??
et puis ca:
ca marchera jamais !
|
| paranoidandroid | comment faire ça ?
int total=100; char *variable[total]; char phrase="BLABLA BLA BLOBLO"; for (i=0;i<total;i++) { je veux inserer chaque valeur de variable[i] à la phrase ... pour obtenir : "BLABLA BLA variable[i] BLOBLO" } aisni je veux obtenir : "BLABLA BLA variable[1] BLOBLO" "BLABLA BLA variable[2] BLOBLO" "BLABLA BLA variable[3] BLOBLO" ... "BLABLA BLA variable[100] BLOBLO" merci ... |


