用c语言输出歌的名字
用c语言输出歌的名字如题如:D\music\童话.MP3->童话
程序代码:#include <stdio.h>
#include <string.h>
const char* getfilename( const char* path, char delimit, size_t* n )
{
const char* p1 = strrchr( path, delimit );
const char* p2 = strrchr( path, '.' );
if( !p1 )
p1 = path-1;
if( !p2 || p2<p1 )
p2 = path+strlen(path);
*n = p2 - p1 - 1;
return p1+1;
}
void printfilename( const char* path, char delimit )
{
size_t n;
const char* p = getfilename( path, delimit, &n );
printf( "%.*s\n", n, p );
}
int main()
{
printfilename( "", '/' );
printfilename( "test", '/' );
printfilename( "test.mp3", '/' );
printfilename( "mdeia/test", '/' );
printfilename( "mdeia.1/test", '/' );
printfilename( "mdeia.1/test.mp3", '/' );
printfilename( "/root/media/test.mp3", '/' );
printfilename( "/home/user/music/yesterday.mp3", '/' );
printfilename( "D:\\music\\童话.MP3", '\\' );
return 0;
}
程序代码:test test test test test test yesterday 童话