Transferred from: http://blog.csdn.net/peter_cloud/article/details/9308333
Copyright statement: This article is an original article by the blogger and may not be reproduced without the permission of the blogger.
Unless your originals are considered cross-platform.
It is really convenient to use more system functions in Linux programming, hey, there is no way to get more lazy ~~~~~~
Today I recorded two simple linux system functions that handle file paths, dirname and basename.
head File:
#include <libgen.h>
Function definition:
char * dirname (char * path);
char * basename (char * path);
Function description:
char * dirname (char * path);
Function: Intercept the directory path in path and return.
char * basename (char * path);
Function: Intercept the last file or path name in the de-directory section of path.
return value:
char * dirname (char * path);
Success: Returns the directory path pointer in the interception path.
Fails: returns NULL
char * basename (char * path);
Success: Returns the pointer to the last file or pathname in the truncated directory portion of path.
Fails: returns NULL
sample graph:
path dirname basename
"/ usr / lib" "/ usr" "lib"
"/ usr /" "/" "usr"
"usr" "." "usr"
"/" "/" "/"
"." "." "."
".." "." ".."
Code example:
char * dirc, * basec, * bname, * dname;
char * path = "/ etc / passwd";
dirc = strdup (path);
basec = strdup (path);
dname = dirname (dirc);
bname = basename (basec);
printf ("dirname =% s, basename =% s \ n", dname, bname);
Linux system Functions (DirName, basename) "Go"