如何删除文件路径的最后一部分

海盗

我需要删除文件路径的最后一部分。

例如,如果我有这个文件名"user/doc/file1",我希望能够获得"user/doc/".

胡言乱语

你可能想要这个:

#include <stdio.h>
#include <string.h>

int main()
{
  char string[] = "/user/doc/file1";

  // find pointer to last '/' in string
  char *lastslash = strrchr(string, '/');

  if (lastslash)           // if found
    *(lastslash + 1) = 0;  //   terminate the string right after the '/'

  printf ("string = %s\n", string);
}

输出

string = /user/doc/

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章