How to extract filename from path

后端 未结 10 1917
遥遥无期
遥遥无期 2020-12-15 03:26

There should be something elegant in Linux API/POSIX to extract base file name from full path

10条回答
  •  甜味超标
    2020-12-15 03:37

    Here's an example of a one-liner (given char * whoami) which illustrates the basic algorithm:

    (whoami = strrchr(argv[0], '/')) ? ++whoami : (whoami = argv[0]);
    

    an additional check is needed if NULL is a possibility. Also note that this just points into the original string -- a "strdup()" may be appropriate.

提交回复
热议问题