VC从文件全路径中获取文件名和扩展名方法(CString)
时间:2010-03-18 来源:清凌一色两滴泪
方法一:
CString CPrintImage::GetFileTitleFromFileName(CString FileName,CString& ExtendName) //获取文件名和扩展名
{
int Where;
Where=FileName.ReverseFind('\\');
if(Where==-1)
{
Where=FileName.ReverseFind('/');
}
CString FileTitle=FileName.Right(FileName.GetLength()-1-Where);
int Which=FileTitle.ReverseFind('.');
ExtendName=FileTitle.Right(FileTitle.GetLength()-Which-1);
if (Which!=-1)
{
FileTitle=FileTitle.Left(Which);
}
return FileTitle;
}
调用方法:
CString a,b,c;
a="c:\\kele8\\shootman2\\vision\\123.exe";
b=this->GetFileTitleFromFileName(a,c);
AfxMessageBox(b); //弹出123.exe
AfxMessageBox(c); //弹出exe
方法二:
CString CPrintImage::GetFileTitleFromFileName(CString FileName, BOOL Ext)
{
int Where;
Where = FileName.ReverseFind('\\');
if (Where == -1)
Where = FileName.ReverseFind('/');
CString FileTitle = FileName.Right(FileName.GetLength() - 1 - Where);
if (!Ext)
{
int Which = FileTitle.ReverseFind('.');
if (Which != -1)
FileTitle = FileTitle.Left(Which);
}
return FileTitle;
}
调用方法:
CString a,b,c;
a="c:\\kele8\\shootman2\\vision\\123.exe";
b=this->GetFileTitleFromFileName(a,TRUE);
c=this->GetFileTitleFromFileName(a,FALSE);
AfxMessageBox(b); 弹出123.exe
AfxMessageBox(c); 弹出123