-2

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 "";
}

}

a=getFilename(ファイル名); // ファイル名は画像です

4

1 に答える 1

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 "";

}

于 2013-09-08T07:41:17.570 に答える