8

私は次のようなものを使用しています:

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()BOOSTのようなサードパーティのライブラリを使用せずにLinuxの場合と同じように、このディレクトリの絶対パスを決定する方法はありますか?各ファイルへの絶対パスを出力したいのですが。

4

2 に答える 2

10

関数を参照してくださいGetFullPathName

于 2012-09-11T00:38:59.913 に答える
4

GetFullPathNameを試すことができます

SetCurrentDirectoryまたは、とを使用できますGetCurrentDirectory。これを行う前に現在のディレクトリを保存して、後で戻ることができるようにすることをお勧めします。

どちらの場合も、検索ディレクトリのフルパスを取得するだけで済みます。API呼び出しは遅いです。ループ内では、文字列を組み合わせるだけです。

于 2012-09-11T00:43:35.347 に答える