C++ 完整的路径怎么获取文件名

2025-03-13 20:01:30
推荐回答(3个)
回答1:

C风格:

1

char *p = strrchr(path.c_str(), '/')

p是path里最后一个'/'的地址。然后

1

string s(p + 1);

,s就是"world.shp"了。

C++风格:

1

int pos = path.find_last_of('/');

pos就是最后一个'/'的下标。

然后

1

string s(path.substr(pos + 1) );

s就是"world.shp"了。

回答2:

CString GetFileName(CString sFullName)
{
    CString s(sFullName);
    int nPos = s.ReverseFind(_T('\\'));
    if (nPos != -1)
        s.Delete(nPos, s.GetLength() - nPos);
    return s;
}


调用方法 CString fn = GetFileName(ss);

回答3:

iPos = ss.ReverseFind(_T("\"));
CString str = ss.Right(ss.GetLength() - iPos - 1);