grandmetre
Mitglied
Hallo zusammen,
Vorab gesagt, es ist das erste mal, dass ich auf diese Weise mit Strings unter C arbeite.
Ich habe Probleme beim Benutzen von strtok in Kombination mit strcpy. Ich möchte einen String von Attributen wie
so aufteilen, dass ich eine liste von structs in der Form
füllen kann.
ich gehe dabei wie folgt vor:
und initStringArray sieht so aus:
leider bekomme ich schon bei der Ausgabe: result "..." must be equal "....." unterschiedlicher ergebnisse.
woher kann das kommen? Oder gibts es eine viel elegantere Methode die Problemstellung zu lösen?
Danke schonmal für eure Antworten
Gruß grand
Vorab gesagt, es ist das erste mal, dass ich auf diese Weise mit Strings unter C arbeite.
Ich habe Probleme beim Benutzen von strtok in Kombination mit strcpy. Ich möchte einen String von Attributen wie
Code:
"value=312544;name=fred;id=179asd45;dataType=string;entityType=person"
so aufteilen, dass ich eine liste von structs in der Form
Code:
typedef struct attribute {
string name;
string value;
}attribute;
füllen kann.
ich gehe dabei wie folgt vor:
Code:
char delims[] = ";";
char *result = strtok(dataString, delims);
int i;
char *stringAttributes[numValues];
initStringArray(stringAttributes, numValues);
if(debug)printf("stringAttribute-Array initialized\n");
for (i=0; result != NULL; i++){
strcpy(stringAttributes[i], result);
if(debug)printf("result \"%s\" must equal current result: %s\n", result, stringAttributes[i]);
result = strtok( NULL, delims );//set the pointer in the strtok-funktion to the next delimiter
}
strcpy(delims, "=");
if(debug)printf("copying values\n");
for(i=0; i < numValues; i++){
if(debug)printf("splitting the attribute\n");
result = strtok(stringAttributes[i], delims);
if(debug)printf("copying name: %s\n", result);
strcpy(data[i].name, result);
result = strtok(NULL, delims);
if(result == NULL)continue; //for the case the string contains something like: dataType=int,name=;value=foobar
if(debug)printf("copying value: %s\n", result);
strcpy(data[i].value, result);
}
und initStringArray sieht so aus:
Code:
int initStringArray(char *arr[], int arrLength){
int i;
char string[50*arrLength];
for(i=0; i < arrLength; i++){
arr[i] = &string[50*i];
bzero(arr[i], 50);
}
return 0;
}
leider bekomme ich schon bei der Ausgabe: result "..." must be equal "....." unterschiedlicher ergebnisse.
woher kann das kommen? Oder gibts es eine viel elegantere Methode die Problemstellung zu lösen?
Danke schonmal für eure Antworten
Gruß grand