c++ how to remove filename from path string

前端 未结 5 1447
后悔当初
后悔当初 2020-12-09 02:53

I have

const char *pathname = \"..\\somepath\\somemorepath\\somefile.ext\";

how to transform that into

\"..\\somepath\\som         


        
5条回答
  •  清歌不尽
    2020-12-09 03:35

    use strrchr() to find the last backslash and strip the string.

    char *pos = strrchr(pathname, '\\');
    if (pos != NULL) {
       *pos = '\0'; //this will put the null terminator here. you can also copy to another string if you want
    }
    

提交回复
热议问题