WIN32_IND_DATA-获取绝对路径

WIN32_FIND_DATA - Get the absolute path

本文关键字:路径 DATA- IND WIN32 获取      更新时间:2023-10-16

我使用的是这样的东西:

std::string tempDirectory = "./test/*";
WIN32_FIND_DATA directoryHandle;
memset(&directoryHandle, 0, sizeof(WIN32_FIND_DATA));//perhaps redundant???
std::wstring wideString = std::wstring(tempDirectory.begin(), tempDirectory.end());
LPCWSTR directoryPath = wideString.c_str();
//iterate over all files
HANDLE handle = FindFirstFile(directoryPath, &directoryHandle);
while(INVALID_HANDLE_VALUE != handle)
{
    //skip non-files
    if (!(directoryHandle.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
    {
        //convert from WCHAR to std::string
        size_t size = wcslen(directoryHandle.cFileName);
        char * buffer = new char [2 * size + 2];
        wcstombs(buffer, directoryHandle.cFileName, 2 * size + 2);
        std::string file(buffer);
        delete [] buffer;
        std::cout << file;
    }
    if(FALSE == FindNextFile(handle, &directoryHandle)) break;
}
//close the handle
FindClose(handle);

其打印相对目录CCD_ 1中的每个文件的名称。

有没有办法确定这个目录的绝对路径,就像realpath()在Linux上所做的那样,而不涉及任何第三方库,如BOOST?我想打印每个文件的绝对路径。

请参阅GetFullPathName函数。

您可以尝试GetFullPathName

也可以使用SetCurrentDirectoryGetCurrentDirectory。在执行此操作之前,您可能需要保存当前目录,以便之后可以返回到该目录。

在这两种情况下,您只需要获得搜索目录的完整路径。API调用很慢。在循环中,您只需组合字符串。