[英]listing all files in a folder returns empty list
I am referring to this thread to list all files in a folder. 我指的是此线程以列出文件夹中的所有文件。 Here is the function: 这是函数:
vector<string> GetFileList(string path)
{
vector<string> names;
WIN32_FIND_DATA fd;
LPCSTR pathLPCW = path.c_str();
HANDLE hFind = ::FindFirstFile(pathLPCW, &fd);
if(hFind != INVALID_HANDLE_VALUE)
{
do
{
// read all (real) files in current folder
// , delete '!' read other 2 default folder . and ..
if(! (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
{
string fileName(fd.cFileName);
names.push_back(fileName);
}
} while(::FindNextFile(hFind, &fd));
::FindClose(hFind);
}
return names;
}
Here is how it gets called in the main: 这是它在主要部分中的调用方式:
int _tmain(int argc, _TCHAR* argv[])
{
string path = "D:\\CTData\\64_Slice_Data\\Helical\\fisrt_helical64_data";
vector<string> fileList = GetFileList(path);
return 0;
}
However, I get 0 files in the list. 但是,我在列表中得到0个文件。 When I was debugging, I found the following line was a false for the if condition. 当我调试时,我发现以下行对于if条件是假的。
if(! (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
I don't quite understand why. 我不太明白为什么。 I obviously have files in the folder. 我显然在文件夹中有文件。 So why am I get a empty list? 那么,为什么我会得到一个空名单? Thanks a lot. 非常感谢。
UPDATE: 更新:
I added GetLastError() following while(::FindNextFile(hFind, &fd)); 我在while(:: FindNextFile(hFind,&fd));之后添加了GetLastError()。 like following: 如下所示:
while(::FindNextFile(hFind, &fd));
lastError = GetLastError();
and I got ERROR_NO_MORE_FILES' (18)
error. 而我得到ERROR_NO_MORE_FILES' (18)
错误。 I am confused, because it looks like I have many files in the folder? 我很困惑,因为看起来文件夹中有很多文件?
Your problem is the filter. 您的问题是过滤器。 basically "D:\\\\CTData\\\\64_Slice_Data\\\\Helical\\\\fisrt_helical64_data"
points to the directory itself. 基本上, "D:\\\\CTData\\\\64_Slice_Data\\\\Helical\\\\fisrt_helical64_data"
指向目录本身。 you need to change it to "D:\\\\CTData\\\\64_Slice_Data\\\\Helical\\\\fisrt_helical64_data\\\\*.*"
您需要将其更改为"D:\\\\CTData\\\\64_Slice_Data\\\\Helical\\\\fisrt_helical64_data\\\\*.*"
If I was writing it I would also avoid returning a vector and pass the pointer to the function to be filled. 如果我正在编写它,那么我也将避免返回向量并将指针传递给要填充的函数。
#include <Windows.h>
#include <vector>
#include <string>
using namespace std;
typedef vector<wstring> TFileList;
void GetFileList(wstring path, TFileList* pFileList)
{
WIN32_FIND_DATA fd;
HANDLE hFind = ::FindFirstFile(path.c_str(), &fd);
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
// read all (real) files in current folder
// , delete '!' read other 2 default folder . and ..
if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
wstring fileName(fd.cFileName);
pFileList->push_back(fileName);
}
} while (::FindNextFile(hFind, &fd));
::FindClose(hFind);
}
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
{
wstring path = {L"D:\\CTData\\64_Slice_Data\\Helical\\fisrt_helical64_data\\*.*"};
TFileList names;
GetFileList(path, &names);
return 0;
}
imagine if your function returns 1000s of file names. 假设您的函数是否返回1000个文件名。 you don't want to create your list once and then copy it to a new place and then delete the one inside your function. 您不想一次创建列表,然后将其复制到新位置,然后删除函数中的列表。 Also change to main
function to be the new format. 也将main
功能更改为新格式。
Sam 山姆
You must use a wildcard character in the end, from the MSDN documentation : 最后,您必须使用MSDN文档中的通配符:
To examine a directory that is not a root directory, use the path to that directory, without a trailing backslash. 要检查不是根目录的目录,请使用该目录的路径,且不带反斜杠。 For example, an argument of "C:\\Windows" returns information about the directory "C:\\Windows", not about a directory or file in "C:\\Windows". 例如,参数“ C:\\ Windows”返回有关目录“ C:\\ Windows”的信息,而不是有关“ C:\\ Windows”中的目录或文件的信息。 To examine the files and directories in "C:\\Windows", use an lpFileName of "C:\\Windows\\*". 要检查“ C:\\ Windows”中的文件和目录,请使用“ C:\\ Windows \\ *”的lpFileName。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.