自定义函数实现全路径中获取路径及文件名
程序代码:
// vfdff@ vc6.0下编译测试通过 #include <stdio.h> #include <stdlib.h> #include <string.h> char *u_basename(char *s) { int i = 0; for (i = strlen(s); i>=0; i--) { if ((s[i]=='\\') || (s[i]=='/')) { break; } } return &(s[i+1]); } int u_dirname(char *dest, char *src) { int i = 0; for (i = strlen(src); i>=0; i--) { if ((src[i]=='\\') || (src[i]=='/')) { break; } } memcpy(dest, src, i+1); dest[i+1] = 0; return 0; } main() { char *s1="c:\\Welcome\\To\\Beijing.txt"; char *s2="c:/Welcome/To/Beijing.txt"; char *s3="Beijing.txt"; char path[80]; printf( u_basename(s1) ); printf("\n"); printf( u_basename(s2) ); printf("\n"); printf( u_basename(s3) ); printf("\n"); u_dirname(path, s1); printf( "%s\n",path ); u_dirname(path, s2); printf( "%s\n",path ); u_dirname(path, s3); printf( "%s\n",path ); return 0; }