我正在使用类似于这样的东西:
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);
这段代码会打印出相对目录 ./test/*
中每个文件的名称。
有没有办法确定该目录的绝对路径,就像 Linux 上的 realpath()
一样,而不涉及任何第三方库,比如 BOOST?我想打印每个文件的绝对路径。
std::vector<char>
简单来说就是一个动态分配的char
数组,所以如果你使用.resize()
将其大小调整到所需大小,那么你可以调用.data()
来获取指向其初始元素的指针。对于wcstombs
函数而言,这个指针和通过new
分配数组得到的指针没有任何区别。 - James McNellisWIN32_FIND_DATA directoryHandle = { 0 };
来消除 memset。在 C 和 C++ 中都有效。 - MSalters