WIN32_IND_DATA-获取绝对路径
WIN32_FIND_DATA - Get the absolute path
我使用的是这样的东西:
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
也可以使用SetCurrentDirectory
和GetCurrentDirectory
。在执行此操作之前,您可能需要保存当前目录,以便之后可以返回到该目录。
在这两种情况下,您只需要获得搜索目录的完整路径。API调用很慢。在循环中,您只需组合字符串。
相关文章:
- 如何将更多文件夹添加到c++include路径
- 带有特殊路径部分的"std::filesystem::weakly_canonical"失败
- C++A*算法并不总是在路径中具有目标节点
- 从函数角度看ID到文件路径的内部与外部映射
- 如何处理 c++ 中类实现中的"invalid use of non-static data member"?
- boost xml parsingl将xml的路径作为变量发送
- 对于MacOS上的G++,如何添加默认的include目录/usr/local/include和默认的库搜索路径/usr
- 如何使用cppcheck处理半相对包含路径
- 在C++中设置基于操作系统的文件路径
- 基于编译器选项的编译二进制路径
- 按边长度递归搜索图中所有可行路径
- 使用变量值作为 PlaySound 中的路径
- 'string.assign(string.data(), 5)' 是明确定义的还是 UB?
- 如何转换真实路径 CString c++
- 从 GUID 获取 USB 卷路径
- 查找 GCD:并非所有控制路径都返回值
- C++17 文件系统::remove_all 带有通配符路径
- 在带有尾部斜杠的路径上返回 std::filesystem::create_directories() 的值
- 获取当前正在运行的 exe 名称(不是路径)
- 如何从 Skia 路径几何体中获取网格?