S
Ok, hier ist meine Funktion. Übergabeparameter werden hier nicht kontrolliert etc.
Man übergibt: <KEY> <VAL> <INDEX>
#include <stdio.h>
#include <string.h>
#define BUF_SIZE 256
int nMapKeyValue (char *pcName,
char *pcValue,
int nIndex,
char pc_KeyValue[][2][256])
{
int j;
int n_Base = 10;
char pc_IndexBuff[3];
char *pc_ID;
char *pc_Index;
char *pc_Key;
char *pc_Value;
if(!(pc_Index = (char *)calloc(BUF_SIZE, 1))) {
printf("Speicherallozierung fiel aus!");
goto END;
}
if(!(pc_Key = (char *)calloc(BUF_SIZE, 1))) {
printf("Speicherallozierung fiel aus!");
goto END;
}
if(!(pc_Value = (char *)calloc(BUF_SIZE, 1))) {
printf("Speicherallozierung fiel aus!");
goto END;
}
for (j = 0; j < nIndex; j++) {
strncpy(pc_Index,
(const char *)(itoa(j, pc_IndexBuff, n_Base)),
3);
strncat((strcpy(pc_Key, pcName)),
pc_Index,
sizeof(pc_Key));
strncat((strcpy(pc_Value, pcValue)),
pc_Index,
sizeof(pc_Value));
pc_ID = (char *)&pc_KeyValue[j][0];
strcpy(pc_ID,
pc_Key);
printf("%10s %5s: %-20s\n",
pc_Index,
"KEY",
pc_ID);
pc_ID = (char *)&pc_KeyValue[j][1];
strcpy(pc_ID,
pc_Value);
printf("%10s %5s: %-20s\n",
pc_Index,
"VAL",
pc_ID);
}
printf ("----------------------\n");
free(pc_Index);
free(pc_Key);
free(pc_Value);
END:
return 0;
}
int main(int argc, char *argv[])
{
int nIndex = atoi(argv[3]);
int nTmp = 0;
char pc_ID[nIndex][2][256];
nMapKeyValue((char *)argv[1],
(char *)argv[2],
nIndex,
pc_ID);
while (nTmp < nIndex) {
printf("%10i %5s: %-10s\n",
nTmp,
"KEY",
pc_ID[nTmp][0]);
printf("%10i %5s: %-10s\n",
nTmp,
"VAL",
pc_ID[nTmp][1]);
nTmp++;
}
return 0;
}
Für eine elegantere Lösung bin ich sehr dankbar.