一、
1.只獲得路徑字串不包含文件名
TCHAR szFilePath[MAX_PATH + 1]={0};
GetModuleFileName(NULL, szFilePath, MAX_PATH);
(_tcsrchr(szFilePath, _T('\\')))[1] = 0; // 刪除文件名,只獲得路徑字串
CString str_url = szFilePath; // 例如str_url==e:\program\Debug\
---------------------------------------------------------
2.獲得雙斜杠路徑不包含文件名
TCHAR _szPath[MAX_PATH + 1]={0};
GetModuleFileName(NULL, _szPath, MAX_PATH);
(_tcsrchr(_szPath, _T('\\')))[1] = 0;//刪除文件名,只獲得路徑 字串
CString strPath;
for (int n=0;_szPath[n];n++)
{
if (_szPath[n]!=_T('\\'))
{
strPath +=_szPath[n] ;
}
else
{
strPath += _T("\\\\");
}
}
MessageBox(strPath);//輸出==e:\\program\\Debug\\
二、
1:獲取應用程序自身完整路徑文件名
方法1:
#include "stdlib.h"
void main()
{
cout << _pgmptr << endl;
}
方法2:
char szFullPath[MAX_PATH];
ZeroMemory(szFullPath,MAX_PAT);
::GetModuleFileName(NULL,szFullPath,MAX_PATH);
::MessageBox(NULL,szFullPath,"path",MB_ICONINFORMATION);
方法3:
TCHAR szPath[MAX_PATH] = {0};
if(!GetModuleFileName(NULL, szPath, MAX_PATH))
{ return ; }
AfxMessageBox(szPath);
2:如何獲取應用程序所在目錄?
這里值得注意的是很多人都用
GetCurrentDirectory(MAX_PATH, szCurrentPath);
來(lái)獲取。這個(gè)方法并不好,經(jīng)常出錯,比如現在我有一個(gè)程序在d:\test目錄下,現在運行這個(gè)程序后用GetCurrentDirectory得到的是d:\test
。接著(zhù)在程序里用CFileDialog來(lái)打開(kāi)一個(gè)C:\test\test.txt文件后再調用GetCurrentDirectory,那么得到的szCurrentPath就是C:\test而不是d:\test。
推薦用如下方法來(lái)得到當前程序所在目錄比較安全:
void _splitpath( const char *path, char *drive, char *dir, char *fname, char *ext );
函數來(lái)分解開(kāi)始提到的_pgmptr,然后再用
void _makepath( char *path, const char *drive, const char *dir, const char *fname, const char *ext );
函數來(lái)對分解后的路徑進(jìn)行組合。這兩個(gè)函數結合起來(lái)功能強大,使用靈活,基本上所有的有關(guān)目錄和路徑方面的操作都可以搞定。
轉載于:http://hi.baidu.com/wyuanshiy/blog/item/7818a5ec6ffab422269791dc.html
MSDN的用法:
#include <stdlib.h>#include <stdio.h>int main( void ){ char path_buffer[_MAX_PATH]; char drive[_MAX_DRIVE]; char dir[_MAX_DIR]; char fname[_MAX_FNAME]; char ext[_MAX_EXT]; errno_t err; err = _makepath_s( path_buffer, _MAX_PATH, "c", "\\sample\\crt\\", "crt_makepath_s", "c" ); if (err != 0) { printf("Error creating path. Error code %d.\n", err); exit(1); } printf( "Path created with _makepath_s: %s\n\n", path_buffer ); err = _splitpath_s( path_buffer, drive, _MAX_DRIVE, dir, _MAX_DIR, fname, _MAX_FNAME, ext, _MAX_EXT ); if (err != 0) { printf("Error splitting the path. Error code %d.\n", err); exit(1); } printf( "Path extracted with _splitpath_s:\n" ); printf( " Drive: %s\n", drive ); printf( " Dir: %s\n", dir ); printf( " Filename: %s\n", fname ); printf( " Ext: %s\n", ext );}
我自己寫(xiě)了個(gè)合成當前EXE所在目錄某個(gè)文件的完整路徑函數:
void make_full_path(char* s, int nLen, const char *file_name, const char*file_ext){ char szPath[MAX_PATH]={0}; GetModuleFileNameA(NULL, szPath, MAX_PATH); char cDir[100] = ""; char cDrive[10] = ""; char cf[20] = ""; char cExt[10] = ""; _splitpath_s(szPath, cDrive, cDir, cf, cExt); _makepath_s(s, nLen, cDrive, cDir, file_name, file_ext);}
string GetExePath(void){ char szFilePath[MAX_PATH + 1]={0}; GetModuleFileNameA(NULL, szFilePath, MAX_PATH); (strrchr(szFilePath, '\\'))[0] = 0; // 刪除文件名,只獲得路徑字串 string path = szFilePath; return path;}
參數說(shuō)明:
s用來(lái)接收完整路徑;
nLen緩沖區長(cháng)度;
file_name為文件名稱(chēng),不帶后綴;
file_ext為文件后綴。
FILE *f; TCHAR szFilePath[MAX_PATH + 1]={0}; sprintf_s(szFilePath, "%s", g_file_in.c_str()); //GetModuleFileName(NULL, szFilePath, MAX_PATH); (strrchr(szFilePath, '.'))[1] = 0; sprintf_s(szFilePath, "%soutput.txt", szFilePath); fopen_s(&f, szFilePath, "a+"); fwrite(strLog.c_str(), 1, strlen(strLog.c_str()), f); fclose(f);
聯(lián)系客服