目录
一、目的:不想使用公司库,需要将文件放到exe所在的文件夹里面,这样就不需要使用绝对路径了
一、目的:不想使用公司库,需要将文件放到exe所在的文件夹里面,这样就不需要使用绝对路径了
一、参考:
1、c/c++ 获取当前程序(EXE)所在的路径
https://blog.csdn.net/chence19871/article/details/7440196#
①总结:很好用,下面是修改后的程序
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <Windows.h>
#include <string>
using namespace std;
string GetExePath()
{
char szFilePath[MAX_PATH + 1] = { 0 };
GetModuleFileNameA(NULL, szFilePath, MAX_PATH);
/*
strrchr:函数功能:查找一个字符c在另一个字符串str中末次出现的位置(也就是从str的右侧开始查找字符c首次出现的位置),
并返回这个位置的地址。如果未能找到指定字符,那么函数将返回NULL。
使用这个地址返回从最后一个字符c到str末尾的字符串。
*/
(strrchr(szFilePath, '\\'))[0] = 0; // 删除文件名,只获得路径字串//
string path = szFilePath;
return path;
}
//int main_exePathTest(void)
int main()
{
string str= GetExePath();
system("pause");
return 0;
}