C++程序中获取不带文件路径和后缀的文件名

string getName(const char* full_name)
{
	string file_name = full_name;
	const char*  mn_first = full_name;
	const char*  mn_last  = full_name + strlen( full_name );
	if ( strrchr( full_name, '\\' ) != NULL )
		mn_first = strrchr( full_name, '\\' ) + 1;
	else if ( strrchr( full_name, '/' ) != NULL )
		mn_first = strrchr( full_name, '/' ) + 1;
	if ( strrchr( full_name, '.' ) != NULL )
		mn_last = strrchr( full_name, '.' );
	if ( mn_last < mn_first )
		mn_last = full_name + strlen( full_name );
	
	file_name.assign( mn_first, mn_last );

	return file_name;
}

获取后缀名(不包含.)

string getExt(string full_name)
{
	return full_name.rfind(".") == string.npos ? full_name : full_name.substr(full_name.rfind(".")+1);
}


你可能感兴趣的:(C++,String,File,null)