c/c++中过滤文件路经 后缀

#给定一个字符串,比如char *str = /home/magic/DT/improved_trajectory/UCF-101/ApplyEyeMakeup/ApplyEyeMakeup_g01_c01.avi
//我们的任务是得到最后的文件名,而且去掉后缀.avi,然后加上.txt
string path = str;
int p = path.find_last_of("/");  
string path_temp = path.substr(p+1);  //得到path_temp = ×××××.avi
int q = path_temp.rfind(".avi");  //从后往前找
string path_txt = path_temp.substr(0, q) +".txt";    //得到ApplyEyeMakeup_g01_c01.txt


//另外,附上C的版本
char filename[100]="/home/magic/DT/improved_trajectory/UCF-101/ApplyEyeMakeup/ApplyEyeMakeup_g01_c01.avi";
char *extr=strrchr(filename,'.');
if (extr)
{    
    *extr='';    
     extr++;
}
printf("name=%s
", filename);
printf("extr-name=%s
", extr);
return 0;


 

非学无以广才,非志无以成学! 【Magic_chao

原文地址:https://www.cnblogs.com/logo-88/p/9465384.html