7

次のようなWindowsパスからドライブ文字を抽出するWindowsAPI関数はありますか?

U:\path\to\file.txt
\\?\U:\path\to\file.txt

正しく整理しながら

relative\path\to\file.txt:alternate-stream    

等?

4

4 に答える 4

12

PathGetDriveNumberは、パスにドライブ文字がある場合は0〜25('A'〜'Z'に対応)を返し、それ以外の場合は-1を返します。

于 2011-08-19T13:09:29.063 に答える
4

受け入れられた答え(ありがとう!)を組み合わせてPathBuildRootソリューションを完成させるコードは次のとおりです

#include <Shlwapi.h>    // PathGetDriveNumber, PathBuildRoot
#pragma comment(lib, "Shlwapi.lib")

/** Returns the root drive of the specified file path, or empty string on error */
std::wstring GetRootDriveOfFilePath(const std::wstring &filePath)
{
// get drive #      http://msdn.microsoft.com/en-us/library/windows/desktop/bb773612(v=vs.85).aspx
int drvNbr = PathGetDriveNumber(filePath.c_str());

if (drvNbr == -1)   // fn returns -1 on error
    return L"";

wchar_t buff[4] = {};   // temp buffer for root 

// Turn drive number into root      http://msdn.microsoft.com/en-us/library/bb773567(v=vs.85)
PathBuildRoot(buff,drvNbr);

return std::wstring(buff);  
}
于 2012-06-16T02:01:56.027 に答える
3

要件によっては、マウントポイントを取得するためにGetVolumePathNameを検討することもできます。マウントポイントは、ドライブ文字である場合とそうでない場合があります。

于 2014-05-28T00:28:24.550 に答える
0
#include <iostream>
#include <string>

using namespace std;

int main()
{    
    string aux;
    cin >> aux;
    int pos = aux.find(':', 0);
    cout << aux.substr(pos-1,1) << endl;
    return 0;
}
于 2011-08-19T13:12:34.817 に答える