2014-10-05 19 views
1

没有工作,我有这样的代码在我的程序:上市文件:c

char * choosePic(const char * dir) 
{ 
    WIN32_FIND_DATA fdFile; 
    HANDLE hFind = null; 
    char sPath[2048]; 

    sprintf(sPath, "%s\\*.*", dir); 

    if ((hFind = FindFirstFile((LPCWSTR) sPath, &fdFile)) == INVALID_HANDLE_VALUE) 
    { 
     if (GetLastError() == ERROR_FILE_NOT_FOUND) 
     { 
      printf("File not found"); 
     } 
     else 
     { 
      printf("Error with path") 
     } 

     FindClose(hFind); 
     return NULL; 
    } 

    do 
    { 
     if (strcmp((char *) fdFile.cFileName, "*.*") != 0 && strcmp((char *) fdFile.cFileName, ".." != 0) 
     { 
      printf("%s %s", sPath, dir); 
     } 
    } while (FindNextFile(hFind, &fdFile)); 

    FindClose(hFind); 
} 

我给这个函数的路径,choosePic("C:\\Windows"),我只得到:

> Error with path 

我尝试了很多东西,甚至使用管理员权限运行。 没有用。

+0

哪些错误代码,您在Win32 API中得到些什么? – NPE 2014-10-05 06:45:41

+2

'(LPCWSTR)sPath' - sPath不是一个宽字符串,所以将它转换为一个不会使任何工作。如果您正在编译Unicode,请正确使用它,或切换到多字节应用程序。 – 2014-10-05 06:47:11

+0

我不需要Unicode。那么我应该如何处理sPath?因为删除演员给出了一个错误。错误代码是3. – Shtut 2014-10-05 07:00:43

回答

0

从您所使用的强制转换,它看起来像你的项目设置为Unicode(项目属性 - 配置属性\一般),在这种情况下你char变量需要改为宽字符wchar_tsprintf()strcmp()printf()函数需要更改为它们的Unicode等价物 - 请务必注意,当您必须使用强制转换时,您可能会出错。

或者,您可以更改项目Use Multi-Byte Character Set ......

+0

谢谢,它的工作。 – Shtut 2014-10-05 12:27:24