windows 如何将 WIN32_FIND_DATA 转换为字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3375459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to convert WIN32_FIND_DATA to string?
提问by blood
im using WIN32_FIND_DATA to store the data findfirstfile outputs. i want the file location (C:\file) as a string but i don't know how to get it or any other data from it.
我使用 WIN32_FIND_DATA 来存储数据 findfirstfile 输出。我想要文件位置 (C:\file) 作为字符串,但我不知道如何获取它或从中获取任何其他数据。
Edit: here is my code
编辑:这是我的代码
PTSTR pszFileName;
PTSTR pszFileName2[100];
if (search_handle)
{
do
{
pszFileName = file.cFileName;
pszFileName2[loop] = pszFileName;
Sleep(100);
loop++;
std::wcout << file.cFileName << std::endl;
}
while(FindNextFile(search_handle,&file));
CloseHandle(search_handle);
}
回答by asveikau
WIN32_FIND_DATA
is a struct. Check out the cFileName
member.
WIN32_FIND_DATA
是一个结构。查看cFileName
会员。
For example:
例如:
WIN32_FIND_DATA FindData = {0};
HANDLE hFind = FindFirstFile(pszPattern, &FindData);
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
PTSTR pszFileName = FindData.cFileName;
// TODO: Use pszFileName in some way...
} while (FindNextFile(hFind, &FindData));
FindClose(hFind);
}
Update in response to comments
根据评论更新
In this example the storage for the string is on the stack, and the same buffer is used for every call. This means that every FindNextFile()
overwrites the previous string. You will have to make a copy of the string.
在此示例中,字符串的存储位于堆栈中,并且每次调用都使用相同的缓冲区。这意味着每个都会FindNextFile()
覆盖前一个字符串。您将必须制作该字符串的副本。
Since you're using C++ and classes in std
I suggest you store it in std::string
(or better yet, make sure you define UNICODE
and _UNICODE
and use wstring
.) Initializing a new string
class will do the allocation and copying on your behalf.
由于您使用C ++和类std
我建议你将其存储在std::string
(或更好,但请确保您定义UNICODE
和_UNICODE
和使用wstring
。)初始化一个新的string
类会做配置,以您的名义复制。
Alternatively you can copy the string using the typical C techniques (for example: using malloc
+ memcpy
, strdup
, or similar), but it sounds like you might want a refresher in strings, pointers, and memory allocation in C before you get to that.
或者,您可以使用典型的 C 技术(例如:使用malloc
+ memcpy
、strdup
或类似技术)复制字符串,但听起来您可能需要在开始之前复习 C 中的字符串、指针和内存分配。
By the way -- to check for error, your code compares the find handle against NULL
; this is incorrect. FindFirstFile()
returns INVALID_HANDLE_VALUE
(which works out to (HANDLE)-1
) on failure. Additionally, to close the handle you will want to use FindClose()
, and not CloseHandle()
. (A "find handle" isn't really a handle to a kernel object in the same sense that a file handle, handle to a module, or a thread or process handle is. They've just overloaded the type.)
顺便说一句 - 为了检查错误,您的代码将查找句柄与NULL
; 这是不正确的。失败时FindFirstFile()
返回INVALID_HANDLE_VALUE
(结果为(HANDLE)-1
)。此外,要关闭句柄,您需要使用FindClose()
,而不是CloseHandle()
。(“查找句柄”实际上不是内核对象的句柄,与文件句柄、模块句柄或线程或进程句柄的意义相同。它们只是重载了类型。)
回答by Andy
The problem is that you're storing the address of the filename in your array. Each time that FindNextFile()
is called, it replaces the data in the struct with the information for the next file. You need to allocate memory for the string in your array, and then copy the string from the structure to your array (using something like strncpy_s()
, probably).
问题是您将文件名的地址存储在数组中。每次FindNextFile()
调用时,它都会用下一个文件的信息替换结构中的数据。您需要为数组中的字符串分配内存,然后将字符串从结构复制到数组中(strncpy_s()
可能使用类似)。
Your code is just storing the pointer to the filename member of the struct, once per found file. If you look at the address each element in the array is pointing to, they're all pointing to the same place.
您的代码只是存储指向结构的文件名成员的指针,每个找到的文件一次。如果您查看数组中每个元素所指向的地址,就会发现它们都指向同一个地方。