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,再编译就没有警告了。
Over!
Linux:使用libgen.h:basename,dirname相关推荐
- Linux常用基本命令(rename,basename,dirname)
rename:重命名文件, 我下面的操作是在ubuntu16.04发行版 演示的,centos下面的语法有些不同 1,首先,生成1到100命名的.html后缀的文件 ghostwu@dev:~/lin ...
- linux c basename dirname函数 从路径得到 文件名 目录名
函数定义: // /usr/include/libgen.h extern char *__xpg_basename (char *__path) __THROW; #define basename ...
- (转)Linux:使用libgen.h:basename,dirname
Linux:使用libgen.h:basename,dirname basename以及dirname是两个命令: [test1280@localhost ~]$ which basename /bi ...
- Linux C中的basename函数用法示例
函数原型使用man 3 basename可以查看到. 从中可以看出,这个函数和dirname函数联系非常紧密. 下面是相关的源码示例 //gcc -g basename_demo.c -o basen ...
- Linux实现ffmpeg H.265视频编码
Linux实现ffmpeg H.265视频编码 几乎所有观看的视频,数字地面电视,电缆,卫星或互联网上的压缩.原始的,未压缩的视频太大,会浪费太多的带宽.在DVD和Blu-ray之前,有视频CD(VC ...
- 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_tot ...
- linux 没有windows.h头文件_宋宝华: Linux内核编程广泛使用的前向声明(Forward Declaration)...
本文系转载,著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 作者:宋宝华 来源: 微信公众号linux阅码场(id: linuxdev) 前向声明 编程定律 先强调一点:在一切可 ...
- Ubunbtu18.04报错:No rule to make target ‘kernel/include/linux/netfilter/xt_CONNMARK.h‘
报错make[4]:No rule to make target 'kernel/include/linux/netfilter/xt_CONNMARK.h'报错原因:WSL文件系统大小写不敏感造成的 ...
- linux 驱动 include .h 路径 control,linux内核中的MFD子系统
分析用的内核版本为5.1.3 1.MFD全称 Multi-function Device,多功能设备 2. 为何会出现MFD子系统 由于出现了一类具有多种功能的外围设备或cpu内部集成的硬件模块 3. ...
- 【Linux 内核】调度器 ⑧ ( 进程优先级源码 include\linux\sched\prio.h | 进程分类 | 实时进程 | 普通进程 | 进程优先级数值 | 0 ~ 99 实时进程 )
文章目录 一.Linux 内核进程优先级源码 二.进程分类 三.进程优先级数值 ( 0 ~ 99 实时进程 | 100 ~ 139 普通进程 ) 在之前的博客 [Linux 内核]进程管理 - 进程优 ...
最新文章
- 低能量社交时代,我们都成了小心翼翼的「舔狗」
- 直播预告丨爆款独立站如何利用数据提升经营效率?
- css相关笔记(五)
- 使用命令行导出 SQL Server 数据层应用程序
- 前端学习(2768):上拉加载
- 下列关于html5表单的多样输入方式,IT兄弟连 HTML5教程 HTML5表单 多样的输入类型1...
- HTTP协议理解——计算机网络
- Java基础篇:如何使用continue语句
- redhat7 上安装dummynet
- 异常:请将注册表值...
- 外设适配-TI954 记录
- 利用SPA(SQL Performance Analyzer)对比两个SQL Tuning Set
- 微信小程序中的列表渲染
- SlickEdit 使用技巧大收集
- h5+js实现本地文件读取和写入-web前端教程
- Linux 安装 Hive教程
- 心理学推荐书籍——《九型人格》
- 保险H5页面、新增模板设计记录
- 第九章 贝叶斯信念网络分类
- 易观数科代码埋点、全埋点、可视化埋点