Linux:使用libgen.h:basename,dirname
basename以及dirname是两个命令:
[test1280@localhost ~]$ which basename
/bin/basename
[test1280@localhost ~]$ which dirname
/bin/dirname
可以通过:
man 1 basename
man 1 dirname
来查看对应的帮助文档。
对于basename的描述是:
basename - strip directory and suffix from filenames
dirname - strip last component from file name
关于命令请大家自行阅读man手册。
basename以及dirname不仅是命令,而且还是函数,通过include头文件libgen.h即可使用。
Tips:
man 1 xxx 命令
man 2 xxx 系统级接口
man 3 xxx 函数库接口
使用下列man查看basename以及dirname函数:
man libgen.h
man 3 basename
man 3 dirname
basename, dirname - parse pathname components
The functions dirname() and basename() break a null-terminated pathname string into directory and filename components.
In the usual case, dirname() returns the string up to, but not including, the final '/', and basename() returns the component following the final '/'. Trailing '/' characters are not counted as part of the pathname.
关键是下面这句话:
Both dirname() and basename() may modify the contents of path, so it may be desirable to pass a copy when calling one of these functions.
basename以及dirname都有可能修改字符串的内容,所以在调用他们时,尽可能传入一个副本,小心原始数据被破坏哦。
These functions may return pointers to statically allocated memory which may be overwritten by subsequent calls.
多次调用可能导致上一次内容被覆盖。
返回值:
Both dirname() and basename() return pointers to null-terminated strings. (Do not pass these pointers to free(3).)
返回的都是以null结束的字符串。切记不要free。
两个函数都是Thread safety。
注意basename还有个兄弟版:
There are two different versions of basename() - the POSIX version described above, and the GNU version, which one gets after
#define _GNU_SOURCE
#include <string.h>
The GNU version never modifies its argument, and returns the empty string when path has a trailing slash, and in particular also when it is "/". There is no GNU version of dirname().
可不要想当然以为dirname也有兄弟。
附上测试代码:
测试环境:
CentOS 7:
[test1280@localhost ~]$ uname -a
Linux localhost.localdomain 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
[test1280@localhost ~]$ g++ --version
g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-11)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
using namespace std;
int main()
{
char *dirc, *basec, *bname, *dname;
const char *path[] = {
"/usr/lib",
"/usr/",
"usr",
"/",
".",
".."
};
int i;
for (i=0; i<6; i++)
{
dirc = strdup(path[i]);
basec = strdup(path[i]);
dname = dirname(dirc);
bname = basename(basec);
cout<<">>>>>>"<<endl;
cout<<"path:"<<path[i]<<endl;
cout<<"dirname:"<<dname<<endl;
cout<<"basename:"<<bname<<endl;
cout<<"<<<<<<"<<endl<<endl;
free(dirc);
dirc = NULL;
free(basec);
basec = NULL;
}
return 0;
}
输出如下:
[test1280@localhost ~]$ ./main
>>>>>>
path:/usr/lib
dirname:/usr
basename:lib
<<<<<<
>>>>>>
path:/usr/
dirname:/
basename:usr
<<<<<<
>>>>>>
path:usr
dirname:.
basename:usr
<<<<<<
>>>>>>
path:/
dirname:/
basename:/
<<<<<<
>>>>>>
path:.
dirname:.
basename:.
<<<<<<
>>>>>>
path:..
dirname:.
basename:..
<<<<<<
再多说一句,编译过程中遇到这么一个问题:
[test1280@localhost ~]$ g++ -o main main.C
main.C: In function ‘int main()’:
main.C:17:2: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
};
^
main.C:17:2: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
main.C:17:2: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
……
为啥有警告呢?原因在于一开始我是这么定义的:
char *path[] = {
"/usr/lib",
"/usr/",
"usr",
"/",
".",
".."
};
没有加const修饰。
char 代表的意思是指向一个要被修改的字符串,而字面常量都是无法修改的,当然用char 来声明会有警告。
而使用const char *代表,指向一个“我永远不会修改的字符串”。
加上const,再编译就没有警告了。