c++中怎么得到当前目录名字(不带路径)?

2025-04-15 19:27:12
推荐回答(3个)
回答1:

CString GetFileNameByFilePath()
{
char pFileName[MAX_PATH] = {0}; 
//得到程序自身的全路径 
DWORD dwRet = GetModuleFileNameW(NULL, (LPWCH)pFileName, MAX_PATH); 

CString filepath ;
filepath.Format(_T("%s"),pFileName);
 
CString Result; 
int nPos = filepath.ReverseFind(_T('\\'));
if ( nPos != -1 )
{
CString strTemp;
strTemp = filepath.Right( filepath.GetLength() - nPos - 1 );
Result = strTemp.Left(  strTemp.ReverseFind(_T('.')) );
}

return Result;  //返回文件名
}

回答2:

#include
中使用
DWORD WINAPI GetModuleFileName(
_In_opt_ HMODULE hModule,
_Out_ LPTSTR lpFilename,
_In_ DWORD nSize
);
#include
中使用
void _splitpath( const char *path, char *drive, char *dir, char *fname, char *ext );

回答3:

带路径的截取下不就可以了 这么讲究