已知文件名取文件所在目录,该如何处理
已知文件名取文件所在目录
已知文件名取文件所在目录?最简单的办法是从后向前查找 "\\ ".但有时,文件名中没有 "\\ ",这时就要先对文件名进行转换.具体代码如下:
CString GetPathName(const CString strPathFileName)
{
//取得经过处理后的文件名长度
int nLength = GetFullPathName(strPathFileName,0,NULL,NULL);
if( 0 == nLength )
return " " ;
//取得新文件名
CString strPathName ;
char * pszPathName = strPathName.GetBufferSetLength(nLength);
nLength = GetFullPathName(strPathFileName,nLength,pszPathName,NULL);
strPathName.ReleaseBuffer();
if( 0 == nLength )
return " " ;
//取得路径
int nPos = strPathName.ReverseFind( '\\ ');
if(-1 == nPos )
return " ";
strPathName = strPathName.Left(nPos);
return strPathName ;
}
测试代码如下:
void CMy2View::OnDraw(CDC* pDC)
{
CMy2Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CString strMess ;
CString strInput[] = { "1.txt ", "c:\\2.txt ", "d:3.txt ", "e:\\2/4.txt "};
int iCount = sizeof(strInput)/sizeof(strInput[0]) ;
for(int i = 0 ; i < iCount ; i++ )
{
CString strLine ;
CString strOutput = GetPathName(strInput[i]) ;
strLine.Format( "文件名: %s\r\n对应文件夹 %s\r\n\r\n ",strInput[i],strOutput);
strMess += strLine ;
}
CRect r ;
GetClientRect(r);
pDC-> DrawText(strMess,&r,0);
}
结果如下:
文件名: 1.txt
对应文件夹 G:\TEST\2
文件名: c:\2.txt
对应文件夹 c:
文件名: d:3.txt
对应文件夹 D:
文件名: e:\2/4.txt
对应文件夹 e:\2
------解决方案--------------------
用以下运行时库函数也可以的。
Break a path name into components.
void _splitpath(
const char *path,
char *drive,
char *dir,
char *fname,
char *ext
);
void _wsplitpath(
const wchar_t *path,
wchar_t *drive,
wchar_t *dir,
wchar_t *fname,
wchar_t *ext
);
Parameters
path
Full path
drive
Optional drive letter, followed by a colon (:)
dir
Optional directory path, including trailing slash. Forward slashes ( / ), backslashes ( \ ), or both may be used.
fname
Base filename (no extension)
ext
Optional filename extension, including leading period (.)
已知文件名取文件所在目录?最简单的办法是从后向前查找 "\\ ".但有时,文件名中没有 "\\ ",这时就要先对文件名进行转换.具体代码如下:
CString GetPathName(const CString strPathFileName)
{
//取得经过处理后的文件名长度
int nLength = GetFullPathName(strPathFileName,0,NULL,NULL);
if( 0 == nLength )
return " " ;
//取得新文件名
CString strPathName ;
char * pszPathName = strPathName.GetBufferSetLength(nLength);
nLength = GetFullPathName(strPathFileName,nLength,pszPathName,NULL);
strPathName.ReleaseBuffer();
if( 0 == nLength )
return " " ;
//取得路径
int nPos = strPathName.ReverseFind( '\\ ');
if(-1 == nPos )
return " ";
strPathName = strPathName.Left(nPos);
return strPathName ;
}
测试代码如下:
void CMy2View::OnDraw(CDC* pDC)
{
CMy2Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CString strMess ;
CString strInput[] = { "1.txt ", "c:\\2.txt ", "d:3.txt ", "e:\\2/4.txt "};
int iCount = sizeof(strInput)/sizeof(strInput[0]) ;
for(int i = 0 ; i < iCount ; i++ )
{
CString strLine ;
CString strOutput = GetPathName(strInput[i]) ;
strLine.Format( "文件名: %s\r\n对应文件夹 %s\r\n\r\n ",strInput[i],strOutput);
strMess += strLine ;
}
CRect r ;
GetClientRect(r);
pDC-> DrawText(strMess,&r,0);
}
结果如下:
文件名: 1.txt
对应文件夹 G:\TEST\2
文件名: c:\2.txt
对应文件夹 c:
文件名: d:3.txt
对应文件夹 D:
文件名: e:\2/4.txt
对应文件夹 e:\2
------解决方案--------------------
用以下运行时库函数也可以的。
Break a path name into components.
void _splitpath(
const char *path,
char *drive,
char *dir,
char *fname,
char *ext
);
void _wsplitpath(
const wchar_t *path,
wchar_t *drive,
wchar_t *dir,
wchar_t *fname,
wchar_t *ext
);
Parameters
path
Full path
drive
Optional drive letter, followed by a colon (:)
dir
Optional directory path, including trailing slash. Forward slashes ( / ), backslashes ( \ ), or both may be used.
fname
Base filename (no extension)
ext
Optional filename extension, including leading period (.)