المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : wcstok [modified]



C++ Programming
01-14-2011, 06:30 AM
Here is a sample code which works just fine:

#include #include #define malloc(s) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, s)
#define free(m) HeapFree(GetProcessHeap(), 0, m)
#pragma warning(disable: 4996)

wchar_t *test()
{
wchar_t aaa[] = L"aaaa,bbbb,cccc,dddd";
static wchar_t *lol = (wchar_t *)malloc(sizeof(wchar_t *) * wcslen(aaa) + 1);
wcscpy(lol, aaa);
return lol;
}

int main(int argc, char **argv)
{
wchar_t *t;
int i;
wchar_t *stuff = test();
t = wcstok(stuff, L",");
for(i = 0; t; t = wcstok(NULL, L","), i++)
{
wprintf(L"::%s\n", t);
}
return 0 }

This prints
::aaaa
::bbbb
::cccc
::dddd

Now, here is sample code which just dont work and i got no idea why. Its a wcstok inside of wcstok loop.

#include #include #define malloc(s) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, s)

#define free(m) HeapFree(GetProcessHeap(), 0, m)

#pragma warning(disable: 4996)

wchar_t *test()
{
wchar_t aaa[] = L"aaaa,bbbb,cccc,dddd";
static wchar_t *lol = (wchar_t *)malloc(sizeof(wchar_t *) * wcslen(aaa) + 1);
wcscpy(lol, aaa);
return lol;
}

wchar_t *lolz(wchar_t *lol)
{
int i = 2;
static wchar_t *stuff = (wchar_t *)malloc(sizeof(wchar_t *) * (wcslen(lol) + MAX_PATH)*i);
wcscpy(stuff, L" ");
while(i > 0)
{
wcscat(stuff, lol);
wcscat(stuff, L",");
i--;
}
return stuff;
}

int main(int argc, char **argv)
{
wchar_t *t;
int i;
wchar_t *stuff = test();
t = wcstok(stuff, L",");
for(i = 0; t; t = wcstok(NULL, L","), i++)
{
wprintf(L"::%s\n", t);
wchar_t *get = lolz(t);
wchar_t *t2;
int i2;
t2 = wcstok(get, L",");
for(i2 = 0; t2; t2 = wcstok(NULL, L","), i2++)
{
wprintf(L":%s\n", t2);
}
wprintf(L"End for: %s [%d]\n\n", t, i);
}

return 0;
}
this should print:
::aaaa
:aaaa
:aaaa
end for aaaa
::bbbb
:bbbb
:bbbb
end for bbbb

You can see from the code, like, for each main tokens it should get subtokens, but it prints only:
::aaaa
:aaaa
:aaaa
end for aaaa

and thats it. Why? thanks
011011010110000101100011011010000110100101101110
0110010101110011
modified on Thursday, January 13, 2011 10:19 AM