• 【C语言】basename函数


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

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

    原型

    如下:

    char *basename(char *path);

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

    例:

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

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

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

    例如:

    1. char path[] = "/home/user/file.txt";
    2. 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:

    1. #include
    2. 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():

    1. #include
    2. #include
    3. #include
    4. int main() {
    5. char path[] = "/usr/local/bin/myapp"; // A mutable copy since basename may modify it
    6. char *base = basename(path);
    7. printf("Base name: %s\n", base);
    8. return 0;
    9. }

    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`函数的原型:

    1. #include
    2. char *basename(char *path);

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

    1. #include
    2. #include
    3. #include
    4. int main() {
    5. char path[] = "/usr/local/bin/myapp"; // 可变副本,因为basename可能会修改它
    6. char *base = basename(path);
    7. printf("基本文件名:%s\n", base);
    8. return 0;
    9. }

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

  • 相关阅读:
    [附源码]java毕业设计濒危物种科普系统
    如何对低代码/无代码平台进行分类?
    什么是 CSRF 、原理及其解决方式
    面试题:Java序列化与反序列化
    Elasticsearch字段类型与类型区别
    四.STM32F030C8T6 MCU开发之利用 TIM1+ADC1+DMA1 实现5路(3路外部电压模拟信号+内部2路信号)采集
    2022.7.25-7.31 AI行业周刊(第108期):值钱比赚钱更重要
    ubuntu22的make如何降级
    VEX —— Functions|Math
    MySQL中的DML操作
  • 原文地址:https://blog.csdn.net/eidolon_foot/article/details/133157684