通过文件路径获取路径中的文件名。
函数声明:
PTSTR PathFindFileName(
__in PTSTR pPath
);
参数:
PTSTR pPath
为指向文件路径字符串的指针
返回:
若成功,返回指向文件名的指针;反之,返回指向路径起始的指针
备注:
PathFindFileName既支持Windows下的反斜杠,也支持Unix下的斜杠,以及还支持斜杠和反斜杠的组合
实例:
#include "stdafx.h"
#include <stdio.h>
#include <Shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
int main()
{
char path[] = "C:\\Windows\\System32/notepad.exe";
/* will output "notepad.exe" */
printf("%s\n", PathFindFileName(path));
getchar();
return 0;
}
其中"char *" 类型的实参与 "LPCWSTR" 类型的形参不兼容"问题,在编译器,修改如下即可,Unicode字符集 改为 多字节字符集:
输出notepad.exe: