(转)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,再編譯就沒有警告了。
轉載于:https://www.cnblogs.com/liujiacai/p/9006952.html
總結
以上是生活随笔為你收集整理的(转)Linux:使用libgen.h:basename,dirname的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python模块--json \ pic
- 下一篇: gcc: internal compil