关于这个问题,大家有什么思路吗?

//设计一个函数,能够获得程序名字
//设计一个函数,能够获得程序路径
//以示范程序来说
//C:\Users\zozoz\Documents\Visual Studio 2008\Projects\Debug\linreg.exe
//程序名字  linreg.exe
//路径      C : \Users\zozoz\Documents\Visual Studio 2008\Projects\Debug\

关于这个问题,大家有什么思路吗?这节课主要围绕int main(int count,char* c_arg[])来讲,我估计是和这个函数有关,但我不会写

你的思路呢?

可以使用Path类中的GetFileName函数来获取程序名字,使用Path类中的GetDirectoryName函数来获取程序路径。
int main(int count,char* c_arg[])这个东西就是一个模板版可以这么理解,和这个问题没有任何关系

获取程序名可以使用Windows API函数GetModuleFileName,该函数可以获取当前模块的完整路径和文件名,返回值为一个整数,表示实际写入缓冲区的字符数。需要传入一个字符缓冲区和缓冲区的大小,函数将程序名写入该缓冲区中。具体实现代码如下:

#include <windows.h>

std::string GetProgramName()
{
    char buf[MAX_PATH];
    GetModuleFileName(NULL, buf, MAX_PATH);
    std::string fullPath(buf);
    size_t pos = fullPath.find_last_of("\\");
    return fullPath.substr(pos + 1);
}

获取程序路径可以使用C++标准库中的filesystem库中的path类和current_path函数,该类可以表示文件路径,current_path函数返回当前进程的工作目录。具体实现代码如下:

#include <filesystem>

std::string GetProgramPath()
{
    std::filesystem::path fullPath = std::filesystem::current_path();
    return fullPath.string();
}

需要注意的是,需要在文件头中添加相应的头文件。
使用这两个函数可以获得程序名和程序路径,示例代码如下:

#include <iostream>

int main()
{
    std::string programName = GetProgramName();
    std::string programPath = GetProgramPath();
    std::cout << "Program Name: " << programName << std::endl;
    std::cout << "Program Path: " << programPath << std::endl;
    return 0;
}

望采纳

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(int argc, char** argv)
{
    string path = argv[0];
    cout << "path:" << path << endl;
    int start = path.find_last_of("\\");
    // 从字符串尾反向搜索
    int end = path.find(".exe");
    cout << "name:" << path.substr(start, end - start);
    return 0;
}