列出文件夹 returns 空列表中的所有文件
listing all files in a folder returns empty list
我指的是 this thread 列出文件夹中的所有文件。这是函数:
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;
}
这是它在主程序中的调用方式:
int _tmain(int argc, _TCHAR* argv[])
{
string path = "D:\CTData\64_Slice_Data\Helical\fisrt_helical64_data";
vector<string> fileList = GetFileList(path);
return 0;
}
但是,我在列表中得到 0 个文件。当我调试时,我发现下面这行 if 条件是错误的。
if(! (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
我不太明白为什么。我明明文件夹里有文件。那么为什么我得到一个空列表?非常感谢。
更新:
我在 while(::FindNextFile(hFind, &fd)) 之后添加了 GetLastError();如下所示:
while(::FindNextFile(hFind, &fd));
lastError = GetLastError();
我遇到了 ERROR_NO_MORE_FILES' (18)
错误。我很困惑,因为我的文件夹里好像有很多文件?
你的问题是过滤器。基本上 "D:\CTData\64_Slice_Data\Helical\fisrt_helical64_data"
指向目录本身。您需要将其更改为 "D:\CTData\64_Slice_Data\Helical\fisrt_helical64_data\*.*"
如果我正在编写它,我也会避免返回一个向量并将指针传递给要填充的函数。
#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;
}
想象一下,如果您的函数 returns 1000 个文件名。您不想创建一次列表,然后将其复制到新位置,然后删除函数中的列表。
同时更改为 main
函数以成为新格式。
山姆
最后必须使用通配符,来自MSDN documentation:
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". To examine the files and directories in "C:\Windows", use an lpFileName of "C:\Windows\*".
我指的是 this thread 列出文件夹中的所有文件。这是函数:
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;
}
这是它在主程序中的调用方式:
int _tmain(int argc, _TCHAR* argv[])
{
string path = "D:\CTData\64_Slice_Data\Helical\fisrt_helical64_data";
vector<string> fileList = GetFileList(path);
return 0;
}
但是,我在列表中得到 0 个文件。当我调试时,我发现下面这行 if 条件是错误的。
if(! (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
我不太明白为什么。我明明文件夹里有文件。那么为什么我得到一个空列表?非常感谢。
更新:
我在 while(::FindNextFile(hFind, &fd)) 之后添加了 GetLastError();如下所示:
while(::FindNextFile(hFind, &fd));
lastError = GetLastError();
我遇到了 ERROR_NO_MORE_FILES' (18)
错误。我很困惑,因为我的文件夹里好像有很多文件?
你的问题是过滤器。基本上 "D:\CTData\64_Slice_Data\Helical\fisrt_helical64_data"
指向目录本身。您需要将其更改为 "D:\CTData\64_Slice_Data\Helical\fisrt_helical64_data\*.*"
如果我正在编写它,我也会避免返回一个向量并将指针传递给要填充的函数。
#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;
}
想象一下,如果您的函数 returns 1000 个文件名。您不想创建一次列表,然后将其复制到新位置,然后删除函数中的列表。
同时更改为 main
函数以成为新格式。
山姆
最后必须使用通配符,来自MSDN documentation:
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". To examine the files and directories in "C:\Windows", use an lpFileName of "C:\Windows\*".