Extract base path from a pathname in C

后端 未结 8 1805
南笙
南笙 2020-12-30 14:21

Question

How do you extract the base path from pathname in C?

Are there any functions built into the C language or the C-Runtime L

8条回答
  •  不思量自难忘°
    2020-12-30 15:00

    Before str is the full path and file name, after str is just the path:

    char dir_ch = '\\'; // set dir_ch according to platform
    char str[] = "C:\\path\\to\\file.c";
    char *pch = &str[strlen(str)-1];
    
    while(*pch != dir_ch) pch--;
    pch++;
    *pch = '\0';
    

提交回复
热议问题