string getFilename(string s){
char sep = '/';
char sepExt='.';
#ifdef _WIN32
sep = '\\';
#endif
size_t i = s.rfind(sep, s.length( ));
if (i != string::npos) {
string fn= (s.substr(i+1, s.length( ) - i));
size_t j = fn.rfind(sepExt, fn.length( ));
if (i != string::npos) {
return fn.substr(0,j);
}else{
return fn;
}
}else{
return "";
}
}
一个的getFileName =(文件名); //文件名是图像
答案 0 :(得分:0)
看起来它提取文件的名称没有它的扩展名和路径:
"/home/user/Documents/someimage.jpg" -> "someimage"
size_t i = s.rfind(sep, s.length( )); // find location of the "/"
if (i != string::npos) {
string fn= (s.substr(i+1, s.length( ) - i)); // extract filename with extension -> "someimage.jpg"
size_t j = fn.rfind(sepExt, fn.length( )); // find location of the extension by looking for "."
if (i != string::npos) {
return fn.substr(0,j); // extract filename -> "someimage"
}else{
return fn;
}
}else{
return "";
}