Linux:使用libgen.h:basename,dirname

2024-03-25 17:08

本文主要是介绍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 functionint 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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/845671

相关文章

Linux线程同步/互斥过程详解

《Linux线程同步/互斥过程详解》文章讲解多线程并发访问导致竞态条件,需通过互斥锁、原子操作和条件变量实现线程安全与同步,分析死锁条件及避免方法,并介绍RAII封装技术提升资源管理效率... 目录01. 资源共享问题1.1 多线程并发访问1.2 临界区与临界资源1.3 锁的引入02. 多线程案例2.1 为

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

Oracle数据库定时备份脚本方式(Linux)

《Oracle数据库定时备份脚本方式(Linux)》文章介绍Oracle数据库自动备份方案,包含主机备份传输与备机解压导入流程,强调需提前全量删除原库数据避免报错,并需配置无密传输、定时任务及验证脚本... 目录说明主机脚本备机上自动导库脚本整个自动备份oracle数据库的过程(建议全程用root用户)总结

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3

使用IDEA部署Docker应用指南分享

《使用IDEA部署Docker应用指南分享》本文介绍了使用IDEA部署Docker应用的四步流程:创建Dockerfile、配置IDEADocker连接、设置运行调试环境、构建运行镜像,并强调需准备本... 目录一、创建 dockerfile 配置文件二、配置 IDEA 的 Docker 连接三、配置 Do

Linux如何查看文件权限的命令

《Linux如何查看文件权限的命令》Linux中使用ls-R命令递归查看指定目录及子目录下所有文件和文件夹的权限信息,以列表形式展示权限位、所有者、组等详细内容... 目录linux China编程查看文件权限命令输出结果示例这里是查看tomcat文件夹总结Linux 查看文件权限命令ls -l 文件或文件夹

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

idea的终端(Terminal)cmd的命令换成linux的命令详解

《idea的终端(Terminal)cmd的命令换成linux的命令详解》本文介绍IDEA配置Git的步骤:安装Git、修改终端设置并重启IDEA,强调顺序,作为个人经验分享,希望提供参考并支持脚本之... 目录一编程、设置前二、前置条件三、android设置四、设置后总结一、php设置前二、前置条件