【C语言】basename函数

192 篇文章 3 订阅
188 篇文章 1 订阅

basename()是一个Linux系统编程常用的C语言库函数,包含在头文件<libgen.h>中。

basename()函数的作用是从一个路径中提取文件名部分。

原型

如下:

char *basename(char *path);

给定一个文件路径,basename会返回指向路径中最后一个'/'后的文件名部分字符串的指针。

例:

路径:/home/user/file.txt
调用:basename("/home/user/file.txt")
返回:"file.txt"

如果路径中不包含'/',则直接返回整个路径字符串。basename()不会修改原输入字符串,只是返回一个指针,指向路径中的文件名部分。

basename()返回的字符串指针是指向原输入路径字符串中的一部分,并没有新分配内存。具体来说,basename()的返回值是指向路径输入字符串中最后一个'/'字符后面的子串,只是将原字符串中的一段取出来返回,没有复制和新分配内存。

例如:

char path[] = "/home/user/file.txt";
char *name = basename(path); 

这里name指向的是path字符串中"file.txt"的首地址,并没有分配新的内存。所以不需要再特别释放name指向的内存。basename()的原型中也没有类似malloc/free那种需要释放内存的函数,它仅仅是返回一个指针而已。

ChatGPT

In C programming, the basename function is used to extract the base filename from a given pathname. This function is a part of the libgen.h library on Unix-like systems such as Linux.
Here's the prototype of the basename function:

#include <libgen.h>

char *basename(char *path);

The function takes a pointer to a character array (C string) that contains the path and returns a pointer to the start of the last component of the path. In other words, it strips off any directory components and returns only the filename portion of the path.
Please note the following when using basename():
- The basename function may modify the contents of path, so if you need to preserve the original path, consider passing a copy of the path to basename.
- The pointer returned by basename may point to internal static storage that may be overwritten by subsequent calls to basename or dirname. Therefore, it's not thread-safe.
- The behavior is undefined if path is a null pointer or points to an empty string.
Here's an example of how to use basename():

#include <stdio.h>
#include <libgen.h>
#include <string.h>

int main() {
    char path[] = "/usr/local/bin/myapp"; // A mutable copy since basename may modify it

    char *base = basename(path);

    printf("Base name: %s\n", base);

    return 0;
}

This program should output:
Base name: myapp
Always refer to your system's documentation for the exact behavior and how to handle different edge cases when using basename. And remember to include any required headers, such as libgen.h, and link against any necessary libraries when compiling your program.

在C语言编程中,`basename`函数用来从给定的文件路径中提取文件名。该函数是Unix-like系统(如Linux)上`libgen.h`库的一部分。
以下是`basename`函数的原型:

#include <libgen.h>

char *basename(char *path);

该函数接受一个字符数组(C字符串)指针,该数组包含路径,并返回指向路径最后一部分(基本文件名)开始的指针。换句话说,它去除路径中的目录组成部分,只返回路径中的文件名部分。
使用`basename`函数时,请注意以下几点:
- basename函数可能会修改`path`的内容,因此如果你需要保留原始路径,请考虑传递路径的副本给`basename`。
- basename返回的指针可能指向内部静态存储空间,该空间可能会被后续对`basename`或`dirname`的调用覆盖。因此,它不是线程安全的。
- 如果`path`是一个空指针或者指向一个空字符串,行为是未定义的。
以下是如何使用`basename`的一个例子:

#include <stdio.h>
#include <libgen.h>
#include <string.h>

int main() {
    char path[] = "/usr/local/bin/myapp"; // 可变副本,因为basename可能会修改它

    char *base = basename(path);

    printf("基本文件名:%s\n", base);

    return 0;
}

这个程序应该输出:
基本文件名:myapp
使用`basename`时,始终参考你系统的文档,准确了解其行为以及如何处理不同的边缘情况。同时记得在编译程序时包含所需的头文件,如`libgen.h`,并链接必要的库文件。

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

109702008

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值