比如c:/data/image.png
你只想获得image这个图像名称,路径与后缀都不要的,这个程序正好适合,且能指定获取的长度。
char* GetFilename(char *fullname)
{
int from,to,i;
char *newstr,*temp;
if(fullname!=NULL)
{
if((temp=strchr(fullname,'.'))==NULL)//if not find dot
newstr = fullname;
else
{
from = strlen(fullname) - 1;
to = (temp-fullname); //the first dot's index;
for(i=from;i--;i<=to)
if(fullname[i]=='.')break;//find the last dot
newstr = (char*)malloc(i+1);
strncpy(newstr,fullname,i);
*(newstr+i)=0;
}
}
//newstr 表示带路径和名称,现在是c/data/image
char name[50] = {""};
char *q = strrchr(newstr,'/') + 1; //q表示image now
strncpy(name,q,40);
return name;
}