C
/*
Dynamic array / parsing example by KriPpLer
*/
#include <windows.h>
#include <cstdio>
#include <string.h> //For strok() function
char **Explode(char *StrIn, const char *Delimiter);
int main ()
{
char data[] ="1||2||3||4||5||6||7||8||9||10||11||12||13||14||15||16"; //Data to parse
char **pToken; //Pointer to our dynamic char array
int c = 0;
printf ("================================================================\n");
printf ("Data : %s\n",data);
printf ("================================================================\n");
pToken = Explode(data,"||"); //
while(pToken[c] != 0) //(While current <> null)
{
printf("str[%d] = %s\n", c,pToken[c]);
++c;
}
free(pToken); //Free up the array
printf ("================================================================\n");
for (;;){ //Loop until closed by user
Sleep(1000);
}
return 0;
}
char **Explode(char *StrIn,const char *Delimiter)
{
int iSize = 10;
char *strInBuf;
char **strOutBuf = (char **)malloc(sizeof(char *) * (iSize + 1));
int c = 0;
strInBuf = strtok(StrIn, Delimiter);
while(strInBuf != 0)
{
if(c == iSize)
{
iSize += 10;
strOutBuf = (char **)realloc(strOutBuf, sizeof(char *) * (iSize + 1));
}
strOutBuf[c] = strInBuf;
strInBuf = strtok(0, Delimiter);
++c;
}
strOutBuf[c] = 0;
return strOutBuf;
}
Alles anzeigen
Syler