(转)Linux:使用libgen.h:basename,dirname
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的更多相关文章
- linux学习笔记之 basename, dirname
前言: basename: 用于打印目录或者文件的基本名称 dirname: 去除文件名中的非目录部分,仅显示与目录有关的内容.dirname命令读取指定路径名保留最后一个/及其后面的字符,删除其他部 ...
- Linux常用基本命令(rename,basename,dirname)
rename:重命名文件, 我下面的操作是在ubuntu16.04发行版 演示的,centos下面的语法有些不同 1,首先,生成1到100命名的.html后缀的文件 ghostwu@dev:~/lin ...
- linux 系统函数之 (dirname, basename)【转】
转自:http://blog.csdn.net/peter_cloud/article/details/9308333 版权声明:本文为博主原创文章,未经博主允许不得转载. 除非你的原件考虑跨平台. ...
- basename, dirname 在C语言中的使用
basename作用是得到特定的路径中的最后一个'/',后面的内容 如/usr/bin,得到的内容就是bin 如果/sdcard/miui_recovery/backup 得到的内容就是backup ...
- 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_ ...
- linux没有 conio.h解决的方式
conio.h不是C标准库中的头文件,在ISO和POSIX标准中均未定义. conio是Console Input/Output(控制台输入输出)的简写,当中定义了通过控制台进行数据输入和数据输出的函 ...
- Linux实现ffmpeg H.265视频编码
Linux实现ffmpeg H.265视频编码 几乎所有观看的视频,数字地面电视,电缆,卫星或互联网上的压缩.原始的,未压缩的视频太大,会浪费太多的带宽.在DVD和Blu-ray之前,有视频CD(VC ...
- [sh]basename&dirname截取路径和文件名&case参数选项
给出全路径,取出路径和文件名 basename使用示例 http://codingstandards.iteye.com/blog/840784 示例一 [root@web ~]# basename ...
- linux的<pthread.h>
转自:http://blog.sina.com.cn/s/blog_66cc44d00100in5b.html Linux系统下的多线程遵循POSIX线程接口,称为pthread.编写Linux下的多 ...
随机推荐
- react+webpack基础学习配置
最近学习react,公司的项目是使用create-react-app来搭建的,而我想重新使用node+mysql+react来搭建一个新的博客. 今天尝试从零开始搭建一个webpack+react项目 ...
- Eclipse快捷键和练习题(倒叙,排序)
1 快捷键 内容辅助键 Alt+/ 自动补齐main方法 main 然后 Alt+/ 自动补齐输出语句 syso 然后 Alt+/ 格式化Ctrl+Shift+f 代码区域右键 -- So ...
- javascript 如何创建只能执行一次的事件。
document.getElementById("myelement").addEventListener("click", handler); // ha ...
- Head First Python之4持久存储
open()用法 # encoding:utf-8 try: # 写模式打开文件,若不存在该文件,则创建 out = open("data.out", "w") ...
- Using Lucene's new QueryParser framework in Solr
Sometime back, I described how I built (among other things) a custom Solr QParser plugin to handle P ...
- IT技术公众号推荐
获取二维码方法:http://open.weixin.qq.com/qr/code/?username=公众账号,例如:cjscwe_2015 目录 全栈 编程语言 前端开发 移动开发 数据库 操 ...
- Delphi 调试连接 任意Android手机/平板/盒子(要安装Google USB Driver,并且还有USB的相关许多文章)
Delphi有时候无法连接调试一些手机,解决方案: 1.安装Google USB Driver 2.通过设备管理器查看手机或平板USB的VID,PID 3.修改你的电脑上的android_winusb ...
- 【git】常用命令
// 下载Git项目git clone url // 显示当前git配置git config --list // 设置用户信息git config --global user.name "& ...
- PageAdmin环境配置要求
1.操作系统要求: Win7/win8/win2008/win2012及以上版本都可以,建议用64位的操作系统,服务器建议选择win2012或以上版本. 2.net framework版本要求: ne ...
- “全栈2019”Java第五十一章:继承与final关键字详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...