我使用的是這樣的:WIN32_FIND_DATA - 獲取絕對路徑
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/*
每個文件的名稱。
有沒有什麼辦法可以確定這個目錄的絕對路徑,就像realpath()
在Linux上沒有任何第三方庫如BOOST一樣?我想打印每個文件的絕對路徑。
您是否要求[UNC](http://en.wikipedia.org/wiki/Uniform_Naming_Convention#Uniform_Naming_Convention)路徑? –
我不認爲我需要這樣一個通用的解決方案。本地路徑現在應該做得很好(如C:\ bla \ blabla \ etc)。 –
不保證有本地路徑。 –