-4

为什么这段代码不起作用?

wchar_t path[300];
wchar_t *PathCreator(wchar_t *dir, wchar_t *fileName)
{
    int j = 0;
    while(j < 300)
    {
        path[j] = '\0';
        j++;
    }
    int i;
    i = 0;
    while(*dir != '*')
    {
        path[i] = *dir;
        i++;
        dir++;
    }
    wchar_t *t = fileName;  
    while(*t != '\0')
    {
        path[i] = *t;
        i++;
        t++;
    }
    path[i] = '\0';
    //std::wcout << path << std::endl;
    return path;
}
int _tmain(int argc, _TCHAR* argv[])
{ 
    std::wcout << PathCreator(L"A",L"B");
}
4

1 回答 1

1

你的循环是错误的,它不检查dir. 所以

while(*dir != '*' && *dir != '\0')
{
    path[i] = *dir;
    i++;
    dir++;
}

不是

while(*dir != '*')
{
    path[i] = *dir;
    i++;
    dir++;
}
于 2012-10-15T11:43:15.027 回答