Windowsにおけるパス付きのファイル名を、「'\'が末尾についた状態のパスの部分」と、「パスの部分を取り払ったファイル名部分」とに、分けます。
PathStripPathと、PathRemoveFileSpec という、2つのWin32APIを使用しています。
#include <shlwapi.h> #pragma comment( lib, "shlwapi.lib" ) void analyze_file_path( const char* file_path_string, string* filename, string* path_ends_with_backslash ) { auto& in = file_path_string; auto& out_path = *path_ends_with_backslash; { int n = strlen( in ); char* buf = new char[ n+1 ]; { strcpy( buf, in ); // ファイル名部分のみを抽出 ::PathStripPathA( buf ); *filename = buf; } { strcpy( buf, in ); // パス部分(最後の\\はつかない)を抽出 ::PathRemoveFileSpecA( buf ); out_path = buf; } delete [] buf; // 末尾に、\\がなかった場合は、追加します。 //(場合というか、ついてこないのでつける) if( out_path.back() != '\\' ) out_path.push_back( '\\' ); } return; }
PathStripPathよりも、入力文字列を変更しないPathFindFileNameを使ったほうがより効率的という説もあります。とかね、まあそういう感じでこの辺のPathxxxx系のAPIはものすごくたくさんあり、いろいろと便利だと思うので、調べておくことをおすすめします。
- アーティスト: ジャンクフジヤマ
- 出版社/メーカー: Victor
- 発売日: 2013/03/13
- メディア: MP3 ダウンロード
- この商品を含むブログ (1件) を見る