MFC中使用CString

CString GetSpecialPath()
{
int nPos;
CString strTempPath;

GetModuleFileName(NULL,strTempPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
strTempPath.ReleaseBuffer ();

nPos=strTempPath.ReverseFind ('\\');
strTempPath=strTempPath.Left (nPos);

return (strTempPath + "\\DemoTxt.txt");
}


非MFC中使用string

#include <string>
#include <iostream>
#include <windows.h>
using namespace std;

string GetSpecialPath()
{
string strTempPath(MAX_PATH,0);//必须这样初始化一块缓冲区空间

GetModuleFileName(NULL,strTempPath.begin(),MAX_PATH);
int pos1=strTempPath.size();
int pos2=strTempPath.find_last_of("\\",string::npos);
strTempPath = strTempPath.substr(0,pos2+1);
strTempPath=strTempPath+ "\\DemoTxt.txt";
return strTempPath;
}



非MFC中使用char buffer[ ];

// Modify ModuleFilePath
// 代码中还有点儿问题,就是想办法怎么将得到的字符串传出去,呵呵2014-12-29 今天才想起来 数组分配在堆上
void GetSpecialPath()
{
char strTempPath[MAX_PATH];
memset(strTempPath,0,MAX_PATH);
GetModuleFileName(NULL,strTempPath,MAX_PATH);
//strrchr(strTempPath,'\\')[1]=0; // 将\字符置0
strrchr(strTempPath,'\\')[1]=0; // 将\后第一个字符置0,看使用情况酌情处理
strcat(strTempPath,"DemoTxt.txt");
}


有一天,我在使用Unicode编程时,且不是在MFC环境下,我有得到了一个新的实现方法,就是使用系统提供的能交叉编译的函数

void GetSpecialPath(wchar_t *filesName) 
{
wchar_t strTempPath[MAX_PATH];

memset(strTempPath,0,MAX_PATH);

GetModuleFileName(NULL,strTempPath,MAX_PATH);


wcsrchr(strTempPath,'\\')[0]=0;
wcsrchr(strTempPath,'\\')[1]=0;
wcscat(strTempPath,L"xmlFiles\\");
wcscat(strTempPath,filesName);
}


非Unicode编程

void GetSpecialPath(){
char strTempPath[MAX_PATH];
memset(strTempPath,0,MAX_PATH);
GetModuleFileNameA(NULL,strTempPath,MAX_PATH);
//strrchr(strTempPath,'\\')[1]=0; // 将\字符置0
strrchr(strTempPath,'\\')[1]=0; // 将\后第一个字符置0,看使用情况酌情处理
strcat(strTempPath,"Crash_Dump.dmp");
}


TCHAR混编模式

void GetSpecialPath(){
TCHAR strTempPath[MAX_PATH];
memset(strTempPath,0,MAX_PATH);
GetModuleFileName(NULL,strTempPath,MAX_PATH);
/*strrchr*/_tcsrchr(strTempPath,'\\')[1]=0; // 将\后第一个字符置0,看使用情况酌情处理
/*strcat*/_tcscat(strTempPath,_T("Crash_Dump.dmp"));
}



注意几个东西的使用方法

char* strchr(const char *str, int ch);         //查找 首次出现的ch字符的位置
char* strrchr(const char *str, int ch); //查找 末次出现的ch字符的位置