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

55 篇文章 3 订阅
3 篇文章 0 订阅
<pre name="code" class="cpp"><span style="white-space:pre">	<span style="white-space:pre">	</span>stringstream name_pcd_sub_suffix;       //去掉后缀名  
<span style="white-space:pre">		</span>stringstream ss_read;       </span><pre name="code" class="cpp">	<span style="white-space:pre">	</span>//完整的路径 文件名 后缀名 的 string, 去掉后缀名  两种方法
		//1  file.substring(0,file.length-3 ;substring函数
		//2 
		int pos = ss_read.str().find_last_of('.'); 
		name_pcd_sub_suffix<<ss_read.str().substr(0,pos);


 
以下转自 <a target=_blank href="http://blog.csdn.net/zhuxiaoyang2000/article/details/6800563">http://blog.csdn.net/zhuxiaoyang2000/article/details/6800563</a>
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;
}

去掉后缀
 
<pre name="code" class="html">string getExt(string full_name)
{
	return full_name.rfind(".") == string.npos ? full_name : full_name.substr(full_name.rfind(".")+1);
}


 



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值