C 列出文件夹中的文件名
我试图在目录中列出已筛选的文件。 如果我使用3个字符的扩展名,代码工作正常 如果我使用4个字符的扩展名,代码就会崩溃 我正在Visual Studio 2012下运行代码 如果我使用C 列出文件夹中的文件名,c,visual-c++,C,Visual C++,我试图在目录中列出已筛选的文件。 如果我使用3个字符的扩展名,代码工作正常 如果我使用4个字符的扩展名,代码就会崩溃 我正在Visual Studio 2012下运行代码 如果我使用 ListDirectoryContents(L"*.txt", &x, pList_Files); 没关系 如果我使用 ListDirectoryContents(L"*.txtx", &x, pList_Files); 代码崩溃 bool ListDirectoryCon
ListDirectoryContents(L"*.txt", &x, pList_Files);
没关系
如果我使用
ListDirectoryContents(L"*.txtx", &x, pList_Files);
代码崩溃
bool ListDirectoryContents
(const wchar_t * sDir, unsigned int * Num_files, wchar_t ** FileList)
{
WIN32_FIND_DATA fdFile;
HANDLE hFind = NULL;
wchar_t sPath[2048];
unsigned int k = 0;
wsprintf(sPath, L"%s", sDir);
if((hFind = FindFirstFile(sPath, &fdFile)) == INVALID_HANDLE_VALUE)
{
wprintf(L"Path not found: [%s]\n", sDir);
return false;
}
do
{
//Find first file will always return "."
// and ".." as the first two directories.
if(wcscmp(fdFile.cFileName, L".") != 0 && wcscmp(fdFile.cFileName, L"..") != 0)
{
//Build up our file path using the passed in
// [sDir] and the file/foldername we just found:
//wsprintf(sPath, L"%s\\%s", sDir, fdFile.cFileName);
wsprintf(sPath, L"%s", fdFile.cFileName);
//Is the entity a File or Folder?
if(fdFile.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY)
{
//wprintf(L"Directory: %s\n", sPath);
ListDirectoryContents(sPath, Num_files, FileList); //Recursion, I love it!
}
else
{
wsprintf(FileList[k], L"%s", fdFile.cFileName);
//wsprintf(FileList[k], L"%s", sPath);
k++;
//wprintf(L"%s \n", sPath);
}
}
}
while(FindNextFile(hFind, &fdFile)); //Find the next file.
FindClose(hFind); //Always, Always, clean things up!
*Num_files = k;
return true;
}
在哪里撞车?使用调试器。您的递归也是错误的:您总是从列表中的0开始索引,覆盖上一次迭代。也许您的搜索中有更多的*.txtx文件,并且您超出了
文件列表的范围
?-告诉我们它在哪里坠毁。