您需要的功能位於Windows中可以使用的標準C庫中。
char theDrive[5],thePath[MAX_PATH],theFilename[MAX_PATH],theExtension[MAX_PATH];
_splitpath(theSCTDataFilename,theDrive,thePath,theFilename,theExtension);
您也可以使用這樣這需要任何字符串,一個char和CStringArray的更一般的符號化功能..
void tokenizeString(CString theString, TCHAR theToken, CStringArray *theParameters)
{
CString temp = "";
int i = 0;
for(i = 0; i < theString.GetLength(); i++)
{
if (theString.GetAt(i) != theToken)
{
temp += theString.GetAt(i);
}
else
{
theParameters->Add(temp);
temp = "";
}
if(i == theString.GetLength()-1)
theParameters->Add(temp);
}
}
CStringArray thePathParts;
tokenizeString("your\complex\path\of\strings\separated\by\slashes",'/',&thePathParts);
這會給你的CString(CStringArray的對象)的數組包含輸入字符串的每個部分。只要知道要分割字符串的分隔符字符串,就可以使用此函數解析主要區塊和次要區塊。
你可以使用* - *(短劃線)來標記你的字符串嗎? –
@TonyTheLion但某些應用程序的參數以其他字符開頭 –
您的意思是您正在嘗試編寫代碼來解析命令行中的程序和參數?這不是非常簡單,因爲在包含空格的路徑中存在歧義。查看[CreateProcess](http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425%28v=vs.85%29.aspx)的文檔,特別是關於'lpApplicationName'參數的文檔。據我所知,沒有Windows API封裝該邏輯,所以你必須自己編寫它。 – Luke