如何正确而且有效地获取文件名的解析

fanguanggao 2011-10-27 10:38:11
CFileDialog dlg(FALSE, NULL, strFileName, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, szFilter, NULL);
if ( dlg.DoModal() == IDOK )
{
CString strpath = dlg.GetPathName();
}
如何解析strpath,来得到具体的是什么文件,即*.*。
...全文
113 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
gameslq 2011-10-27
  • 打赏
  • 举报
回复

TCHAR strFileName[MAX_PATH] = {0};
TCHAR szFilter[] = "*.*";
CFileDialog dlg(FALSE, NULL, strFileName, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT, szFilter, NULL);
if ( dlg.DoModal() == IDOK )
{
CString strpath;
int nPos;

/* //单选文件获取文件名 调用
strpath = dlg.GetFileName();
AfxMessageBox(strpath); */
//多选文件获取文件名
POSITION pos = dlg.GetStartPosition();
while ( pos != NULL )
{
strpath = dlg.GetNextPathName(pos);
nPos = strpath.ReverseFind('\\');
CString cs = strpath.Right(strpath.GetLength() -nPos - 1);

AfxMessageBox(cs);
}


}

用户 昵称 2011-10-27
  • 打赏
  • 举报
回复
size_t l = _tcslen( filename );

while( '\\' != filename[ l ] )
{
l--;
}

AfxMessageBox( filename + l + 1 );
诶呦 2011-10-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 oyljerry 的回复:]
反向查找符号\,截取第一个,后面就是文件名以及后缀
[/Quote]
++
strpath是文件路径的全称,单步调试的时候可以看一下,解析的话如楼上所说
oyljerry 2011-10-27
  • 打赏
  • 举报
回复
反向查找符号\,截取第一个,后面就是文件名以及后缀
clxye 2011-10-27
  • 打赏
  • 举报
回复
你可以直接使用或者参照我写的类,由于处理路径是工作中经常用到的功能,所以我做了一下封装。

FilePathHelper.h
-------------------------------------------------------------------------------------

#pragma once
#include <vector>

class CFilePathHelper
{
public:
CFilePathHelper(void);
~CFilePathHelper(void);
CFilePathHelper(LPCTSTR lpszPathName);
CFilePathHelper(CString &strPathName);

private:
CString m_strPathName; // contains full path and file name
CString m_strFileName; // contains full path name after return
CString m_strFileExt; // contains file ext after return
CString m_strFileTitle; // contains file title after return
CString m_strFolderPath; // contains full path after return

private:
void memsetPath();
void ParsePath();

public:
CString GetPathName() const; // return full path and filename
CString GetFolderPath() const; // return full path
CString GetFileName() const; // return only filename
CString GetFileTitle() const; // return file title
CString GetFileExt() const; // return only ext

BOOL IsFileExist();
BOOL DeleteFile();

BOOL IsFolderExist();
BOOL DeleteFolder();

public:
static CString AppendFolder(LPCTSTR lpszFolderPath, LPCTSTR lpszSubFolderPath);
static CString GetParentFolderPath(LPCTSTR lpszFolderPath);

static BOOL IsFileExist(LPCTSTR lpszPathName);
static BOOL DeleteFile(LPCTSTR lpszPathName);

static BOOL IsFolderExist(LPCTSTR lpszFolderPath);
static BOOL CreateFolder(LPCTSTR lpszFolderPath);
static BOOL DeleteFolder(LPCTSTR lpszFolderPath);

static std::vector<CString> FindFile(LPCTSTR lpszFolderPath, LPCTSTR lpszExt = _T("*.*"));
static CString GetModulePath();
static CString GetTempPath();
};





FilePathHelper.cpp
-------------------------------------------------------------------------------------

#include "StdAfx.h"
#include <direct.h>
#include "FilePathHelper.h"


CFilePathHelper::CFilePathHelper(void)
{
memsetPath();
}


CFilePathHelper::~CFilePathHelper(void)
{
}

CFilePathHelper::CFilePathHelper(LPCTSTR lpszPathName)
{
m_strPathName = lpszPathName;
ParsePath();
}

CFilePathHelper::CFilePathHelper(CString &strPathName)
{
m_strPathName = strPathName;
ParsePath();
}

void CFilePathHelper::memsetPath()
{
m_strPathName.Empty();
m_strFileName.Empty();
m_strFileExt.Empty();
m_strFileTitle.Empty();
m_strFolderPath.Empty();
}

void CFilePathHelper::ParsePath()
{
int nPosStart;

if( m_strPathName.IsEmpty() )
{
return;
}

if( -1 != (nPosStart = m_strPathName.ReverseFind( _T('\\') )) )
{
m_strFolderPath = m_strPathName.Left( nPosStart );
}
else
{
return;
}

if( ++nPosStart < m_strPathName.GetLength() )
{
m_strFileName = m_strPathName.Right( m_strPathName.GetLength() - nPosStart );
}
else
{
return;
}

if( -1 != (nPosStart = m_strFileName.ReverseFind( _T('.') )) )
{
m_strFileTitle = m_strFileName.Left( nPosStart );
}
else
{
return;
}

if( ++nPosStart < m_strFileName.GetLength() )
{
m_strFileExt = m_strFileName.Right( m_strFileName.GetLength() - nPosStart + 1 );
}
else
{
return;
}
}


/******************************************** 实例函数 ***************************************************/

CString CFilePathHelper::GetPathName() const
{
return m_strPathName;
}

CString CFilePathHelper::GetFolderPath() const
{
return m_strFolderPath;
}

CString CFilePathHelper::GetFileName() const
{
return m_strFileName;
}

CString CFilePathHelper::GetFileTitle() const
{
return m_strFileTitle;
}

CString CFilePathHelper::GetFileExt() const
{
return m_strFileExt;
}

BOOL CFilePathHelper::IsFileExist()
{
return IsFileExist( GetPathName() );
}

BOOL CFilePathHelper::DeleteFile()
{
return DeleteFile( GetPathName() );
}

BOOL CFilePathHelper::IsFolderExist()
{
return IsFolderExist( GetFolderPath() );
}

BOOL CFilePathHelper::DeleteFolder()
{
return DeleteFolder( GetFolderPath() );
}



/******************************************** 静态函数 ***************************************************/

BOOL CFilePathHelper::IsFileExist(LPCTSTR lpszPathName)
{
CFileFind finder;
BOOL bResult = finder.FindFile( lpszPathName );

if( bResult )
{
finder.Close();
}

return bResult;
}

BOOL CFilePathHelper::DeleteFile(LPCTSTR lpszPathName)
{
return ::DeleteFile( lpszPathName );
}


BOOL CFilePathHelper::IsFolderExist(LPCTSTR lpszFolderPath)
{
return PathFileExists( lpszFolderPath );
}

BOOL CFilePathHelper::CreateFolder(LPCTSTR lpszFolderPath)
{
return _tmkdir( lpszFolderPath ) != -1;
}

BOOL CFilePathHelper::DeleteFolder(LPCTSTR lpszFolderPath)
{
CFileFind finder;
CString strFolderPath = lpszFolderPath;

BOOL bWorking = finder.FindFile( strFolderPath + _T("\\*.*") );
while( bWorking )
{
bWorking = finder.FindNextFile();
::DeleteFile( finder.GetFilePath() );
}

return ::RemoveDirectory( lpszFolderPath );
}


std::vector<CString> CFilePathHelper::FindFile( LPCTSTR lpszFolderPath, LPCTSTR lpszExt /*= _T("*.*")*/ )
{
CFileFind finder;
std::vector<CString> vecPathName;
CString strFolderPath = lpszFolderPath;

BOOL bWorking = finder.FindFile( strFolderPath + _T("\\") + lpszExt );
while( bWorking )
{
bWorking = finder.FindNextFile();
vecPathName.push_back( finder.GetFilePath() );
}

return vecPathName;
}


CString CFilePathHelper::GetTempPath()
{
TCHAR szPathName[MAX_PATH] = _T("");

::GetTempPath( _countof(szPathName), szPathName );

return szPathName;
}

CString CFilePathHelper::AppendFolder( LPCTSTR lpszFolderPath, LPCTSTR lpszSubFolderPath )
{
CString strFolderPath = lpszFolderPath;
CString strSubFolderPath = lpszSubFolderPath;

if( strSubFolderPath.IsEmpty() ) return strFolderPath;
strFolderPath.TrimRight( _T('\\') );
strFolderPath += _T("\\");
strFolderPath += strSubFolderPath;

return strFolderPath;
}

CString CFilePathHelper::GetParentFolderPath( LPCTSTR lpszFolderPath )
{
CString strFolderPath = lpszFolderPath;

strFolderPath.TrimRight( _T('\\') );
strFolderPath = strFolderPath.Left( strFolderPath.ReverseFind( _T('\\') ) );

return strFolderPath;
}

CString CFilePathHelper::GetModulePath()
{
TCHAR szPathName[MAX_PATH + 1];
CFilePathHelper fileHelper;

if( 0 == GetModuleFileName( NULL, szPathName, MAX_PATH ) ) return _T("");
fileHelper = szPathName;

return fileHelper.GetFolderPath();
}
k_badboy 2011-10-27
  • 打赏
  • 举报
回复
if ( dlg.DoModal() == IDOK )
{
CString strpath = dlg.GetPathName();
CString ext = strrchr(strpath,'.')+1; //扩展名
CString filename = strrchr(strpath,'\\')+1;//文件名
}
fanguanggao 2011-10-27
  • 打赏
  • 举报
回复
//解析CFileDialog的GetPathName文件名,不带扩展名的
CString GetFileName(char* pbuf, CString &strPath)
{
CString strFileName = _T("");
if ( strlen(pbuf) == 0 )
{
return strFileName;
}

int i = 0;
int npos0 = 0, npos1 = 0;
while ( pbuf[i] != '\0')
{
if ( pbuf[i] == '\\')
{
npos0 = i; //记录最后一个'\'的位置
}
if ( pbuf[i] == '.')
{
npos1 = i; //记录'.'的位置
}
i ++;
}

char buf[MAX_PATH];
char sFile[MAX_PATH];

for ( i = 0; pbuf[i] != '\0'; i ++)
{
if ( i < npos0 + 1 )
{
buf[i] = pbuf[i];
}
else
{
buf[i] = '\0';
if ( i < npos1 )
{
sFile[i - npos0 - 1] = pbuf[i];
}
else
{
sFile[i - npos0 - 1] = '\0';
}
}
}

strFileName.Format(_T("%s"), sFile);
strFileName.Trim();
strPath.Format(_T("%s"), buf);
strPath.Trim();

return strFileName;
}

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧