PathFindNextComponentA 函数 (shlwapi.h)

分析路径并返回该路径的第一个反斜杠后面的部分。

语法

LPCSTR PathFindNextComponentA(
  [in] LPCSTR pszPath
);

参数

[in] pszPath

类型: PTSTR

指向包含要分析的路径的以 null 结尾的字符串的指针。 此字符串长度不得超过 MAX_PATH 个字符,加上终止 null 字符。 路径组件由反斜杠分隔。 例如,路径“c:\path1\path2\file.txt”有四个组件:c:、path1、path2 和 file.txt。

返回值

类型: PTSTR

返回指向包含截断路径的以 null 结尾的字符串的指针。

如果 pszPath 指向路径中的最后一个组件,则此函数返回指向终止 null 字符的指针。

如果 pszPath 指向终止 null 字符,或者如果调用失败,则此函数返回 NULL

注解

PathFindNextComponent 将遍视路径字符串,直到遇到反斜杠 (“\”) ,忽略该点之前的所有内容(包括反斜杠),并返回路径的其余部分。 因此,如果路径以反斜杠 ((如 \path1\path2) )开头,则函数只需删除初始反斜杠, (path1\path2) 返回其余部分。

示例

下面的简单控制台应用程序将各种字符串传递给 PathFindNextComponent ,以演示函数识别为路径组件的内容并显示返回的内容。 若要在 Visual Studio 中运行此代码,必须链接到 Shlwapi.lib,并在项目设置的预处理器命令中定义 UNICODE。


#include <windows.h>
#include <iostream>
#include <shlwapi.h>

#pragma comment(lib, "shlwapi.lib")     // Link to this file.

int main()
{
    using namespace std;
   
    PCWSTR path = L"c:\\path1\\path2\\file.txt";
 
    // This loop passes a full path to PathFindNextComponent and feeds the 
    // results of that call back into the function until the entire path has
    // been walked.
    while (path)
    {
        PCWSTR oldPath = path;
        path = PathFindNextComponent(path);
 
        // The path variable pointed to the terminating null character.
        if (path == NULL)
        {
            wcout << L"The terminating null character returns NULL\n\n";
        }
        // The path variable pointed to a path with only one component.
		else if (*path == 0)
        {
            wcout << L"The path " << oldPath 
                  << L" returns a pointer to the terminating null character\n"; 
        }
        else 
        {
            wcout << L"The path " << oldPath << L" returns " << path << L"\n";
        }
    }
 
    // These calls demonstrate the results of different path forms.
    // Note that where those paths begin with backslashes, those
    // backslashes are merely stripped away and the entire path is returned.

    PCWSTR path1 = L"\\path1";

    wcout << L"The path " << path1 << L" returns " 
          << PathFindNextComponent(path1);
        
    PCWSTR path2 = L"\\path1\\path2";

    wcout << L"\nThe path " << path2 << L" returns "
          << PathFindNextComponent(path2);
        
    PCWSTR path3 = L"path1\\path2";
 
    wcout << L"\nThe path " << path3 << L" returns "
          << PathFindNextComponent(path3);
 
    wcout << L"\nThe path " << L"c:\\file.txt" << L" returns "
          << PathFindNextComponent(L"c:\\file.txt");
 
    return 0;
}

OUTPUT:
===========
The path c:\path1\path2\file.txt returns path1\path2\file.txt
The path path1\path2\file.txt returns path2\file.txt
The path path2\file.txt returns file.txt
The path file.txt returns a pointer to the terminating null character
The terminating null character returns NULL

The path \path1 returns path1
The path \path1\path2 returns path1\path2
The path path1\path2 returns path2
The path c:\file.txt returns file.txt

注意

shlwapi.h 标头将 PathFindNextComponent 定义为别名,该别名根据 UNICODE 预处理器常量的定义自动选择此函数的 ANSI 或 Unicode 版本。 将非特定编码别名的使用与非非特定编码的代码混合使用可能会导致不匹配,从而导致编译或运行时错误。 有关详细信息,请参阅 函数原型的约定

要求

要求
最低受支持的客户端 Windows 2000 专业版、Windows XP [仅限桌面应用]
最低受支持的服务器 Windows 2000 Server [仅限桌面应用]
目标平台 Windows
标头 shlwapi.h
Library Shlwapi.lib
DLL Shlwapi.dll (版本 4.71 或更高版本)