日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

(转)Linux:使用libgen.h:basename,dirname

發(fā)布時間:2025/3/15 linux 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (转)Linux:使用libgen.h:basename,dirname 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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

?

來查看對應(yīng)的幫助文檔。

對于basename的描述是:

basename - strip directory and suffix from filenames dirname - strip last component from file name

?

關(guān)于命令請大家自行閱讀man手冊。

basename以及dirname不僅是命令,而且還是函數(shù),通過include頭文件libgen.h即可使用。

Tips:?
man 1 xxx 命令?
man 2 xxx 系統(tǒng)級接口?
man 3 xxx 函數(shù)庫接口

使用下列man查看basename以及dirname函數(shù):

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.

?

?

關(guān)鍵是下面這句話:

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都有可能修改字符串的內(nèi)容,所以在調(diào)用他們時,盡可能傳入一個副本,小心原始數(shù)據(jù)被破壞哦。

These functions may return pointers to statically allocated memory which may be overwritten by subsequent calls.

?

多次調(diào)用可能導(dǎo)致上一次內(nèi)容被覆蓋。

返回值:

Both dirname() and basename() return pointers to null-terminated strings. (Do not pass these pointers to free(3).)

?

返回的都是以null結(jié)束的字符串。切記不要free。

兩個函數(shù)都是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().

?

?

可不要想當(dāng)然以為dirname也有兄弟。

附上測試代碼:

測試環(huán)境:

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?代表的意思是指向一個要被修改的字符串,而字面常量都是無法修改的,當(dāng)然用char?來聲明會有警告。

而使用const char *代表,指向一個“我永遠(yuǎn)不會修改的字符串”。

加上const,再編譯就沒有警告了。

轉(zhuǎn)載于:https://www.cnblogs.com/liujiacai/p/9006952.html

總結(jié)

以上是生活随笔為你收集整理的(转)Linux:使用libgen.h:basename,dirname的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。