Linux system Functions (DirName, basename) "Go"

Source: Internet
Author: User
Tags dname function definition



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"


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.