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,再编译就没有警告了。

(转)Linux:使用libgen.h:basename,dirname的更多相关文章

  1. linux学习笔记之 basename, dirname

    前言: basename: 用于打印目录或者文件的基本名称 dirname: 去除文件名中的非目录部分,仅显示与目录有关的内容.dirname命令读取指定路径名保留最后一个/及其后面的字符,删除其他部 ...

  2. Linux常用基本命令(rename,basename,dirname)

    rename:重命名文件, 我下面的操作是在ubuntu16.04发行版 演示的,centos下面的语法有些不同 1,首先,生成1到100命名的.html后缀的文件 ghostwu@dev:~/lin ...

  3. linux 系统函数之 (dirname, basename)【转】

    转自:http://blog.csdn.net/peter_cloud/article/details/9308333 版权声明:本文为博主原创文章,未经博主允许不得转载. 除非你的原件考虑跨平台. ...

  4. basename, dirname 在C语言中的使用

    basename作用是得到特定的路径中的最后一个'/',后面的内容 如/usr/bin,得到的内容就是bin 如果/sdcard/miui_recovery/backup 得到的内容就是backup ...

  5. 001PHP文件处理——文件处理disk_total_space disk_free_space basename dirname file_exists filetype

    <?php /** * 文件处理disk_total_space disk_free_space basename dirname file_exists filetype */ //disk_ ...

  6. linux没有 conio.h解决的方式

    conio.h不是C标准库中的头文件,在ISO和POSIX标准中均未定义. conio是Console Input/Output(控制台输入输出)的简写,当中定义了通过控制台进行数据输入和数据输出的函 ...

  7. Linux实现ffmpeg H.265视频编码

    Linux实现ffmpeg H.265视频编码 几乎所有观看的视频,数字地面电视,电缆,卫星或互联网上的压缩.原始的,未压缩的视频太大,会浪费太多的带宽.在DVD和Blu-ray之前,有视频CD(VC ...

  8. [sh]basename&dirname截取路径和文件名&case参数选项

    给出全路径,取出路径和文件名 basename使用示例 http://codingstandards.iteye.com/blog/840784 示例一 [root@web ~]# basename ...

  9. linux的<pthread.h>

    转自:http://blog.sina.com.cn/s/blog_66cc44d00100in5b.html Linux系统下的多线程遵循POSIX线程接口,称为pthread.编写Linux下的多 ...

随机推荐

  1. Appium常用API(一)

    Appium作为当下一款移动应用的自动化测试工具,对于测试来说重要性不言可寓,废话不多说,下面总结下它常用的API: 1.contextscontexts(self): Returns the con ...

  2. Mathcad操作tips:函数、符号计算

    函数 1. 利用“:”进行函数定义,如 2. 函数支持range variable输入,如 3. 常用数学函数可以在Insert - Function菜单中寻得. 4. 当不确定某个名字是否是函数名时 ...

  3. BHO插件操作IE浏览器,js调用C#方法

    BHO是IE浏览器的扩展程序,全名Browser Helper Object,文件格式为DLL文件.可对IE浏览器的界面和访问内容进行修改操作.BHO只适用于IE浏览器,对其他任何浏览器都没有作用.( ...

  4. Ocelot Consul

    1首先创建一个json的配置文件,文件名随便取,我取Ocelot.json 这个配置文件有两种配置方式,第一种,手动填写 服务所在的ip和端口:第二种,用Consul进行服务发现 第一种如下: { & ...

  5. SDOI2013直径(树的直径)

    题目描述: 点这里 题目大意: 就是在一个树上找其直径的长度是多少,以及有多少条边满足所有的直径都经过该边. 题解: 首先,第一问很好求,两边dfs就行了,第一次从任一点找距它最远的点,再从这个点找距 ...

  6. Windows系统下安装 CMake

    在安装caffe框架的时候需要用到cmake,特将cmake的安装总结如下: 1 什么是cmake CMake是一个跨平台的编译(Build)工具,可以用简单的语句来描述所有平台的编译过程.CMake ...

  7. vm虚拟机Kali无法拖拽文件解决办法

    vm虚拟机Kali无法拖拽文件解决办法 apt-get updateapt-get install open-vm-tools-desktop fusereboot

  8. OCP 12c最新考试原题及答案(071-8)

    8.(5-4) choose the best answer:You need to produce a report where each customer's credit limit has b ...

  9. jquery.nav.js定位导航滚动插件

    jQuery.nav.js插件代码: /* * jQuery One Page Nav Plugin * http://github.com/davist11/jQuery-One-Page-Nav ...

  10. L365

    When I started my company nine years ago, I was a young, inexperienced founder without much capital. ...