次のようなWindowsパスからドライブ文字を抽出するWindowsAPI関数はありますか?
U:\path\to\file.txt
\\?\U:\path\to\file.txt
正しく整理しながら
relative\path\to\file.txt:alternate-stream
等?
次のようなWindowsパスからドライブ文字を抽出するWindowsAPI関数はありますか?
U:\path\to\file.txt
\\?\U:\path\to\file.txt
正しく整理しながら
relative\path\to\file.txt:alternate-stream
等?
PathGetDriveNumberは、パスにドライブ文字がある場合は0〜25('A'〜'Z'に対応)を返し、それ以外の場合は-1を返します。
受け入れられた答え(ありがとう!)を組み合わせて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);
}
要件によっては、マウントポイントを取得するためにGetVolumePathNameを検討することもできます。マウントポイントは、ドライブ文字である場合とそうでない場合があります。
#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;
}