zoukankan      html  css  js  c++  java
  • c++:读取配置文件

    namespace fs = boost::filesystem;

    fs::path objPath = strFilePath; //  strFilePath--目录  

    objPath/=strFileName;// strFileName--文件名

    if(objPath.is_absplute())

    {

      strFullPath = objPath.string();

      return; 

    }

    /* 如果是相对路径,转换为绝对路径*/

    string strCurDir=CFileUtil::getCurModuleDir();

    if(strCurDir.empty())

    {

      return ;

    }

    fs::path objFullPath = strCurDir;

    objFullPath/=objPath;

    strFullPath=objFullPath.string();

    return ;

    //

    std::string CFileUtil::getCurModuleDir()
    {
    std::string strCurrModuleDirectory;
    enum { BUF_LEN = 1024 };
    char pcDirectory[BUF_LEN];

    ::memset(pcDirectory, 0, sizeof(pcDirectory));
    #if defined(WIN32)
    ::GetModuleFileNameA(NULL, pcDirectory, BUF_LEN);
    #elif defined(__linux)
    readlink("/proc/self/exe", pcDirectory, BUF_LEN);
    #elif defined(_AIX)
    char relativePath[BUF_LEN];
    char psinfo_path[BUF_LEN];
    psinfo_t psinfo;
    sprintf_s(psinfo_path, BUF_LEN, "/proc/%d/psinfo", getpid());
    int fd;
    ssize_t bytes_read;
    fd = open(psinfo_path, O_RDONLY);
    if (fd != -1)
    {
    bytes_read = read(fd, (void *)(&psinfo), sizeof(psinfo_t));
    if (bytes_read > 0)
    {
    char shell_command[BUF_LEN];
    sprintf(shell_command, "which %s 2>/dev/null", psinfo.pr_fname);

    //调用 shell命令从环境变量中查找当前程序所在的绝对路径。
    FILE *fp = popen(shell_command, "r");

    if (fp != NULL)
    {
    fgets(pcDirectory, sizeof(pcDirectory), fp);
    pclose(fp);
    }

    if (pcDirectory[0] != '/') //如果没有找到
    {
    char*** pArgv = (char***)psinfo.pr_argv;

    if (pArgv[0][0][0] == '/')
    {
    strcpy(relativePath, pArgv[0][0]);
    }
    else
    {
    getcwd(relativePath, BUF_LEN);
    strcat(relativePath, "/");
    strcat(relativePath, pArgv[0][0]);
    }
    realpath((const char*)relativePath, (char*)pcDirectory);
    }
    }
    else
    {
    close(fd);
    return "";
    }

    close(fd);
    }
    else
    {
    return "";
    }
    #elif defined(__sun)
    char pCurrentPath[BUF_LEN];
    char pExecPath[BUF_LEN];
    const char *p = getexecname();
    getcwd(pCurrentPath, BUF_LEN);
    strcpy(pExecPath, p);
    if (pExecPath[0] == '/')
    {
    strcpy(pcDirectory, pExecPath);
    }
    else
    {
    strcat(pCurrentPath, "/");
    strcat(pCurrentPath, pExecPath);
    strcpy(pcDirectory, pCurrentPath);
    }
    #endif
    for (int i = (int)strlen(pcDirectory); i >= 0; i--)
    {
    #if defined(WIN32)
    if (pcDirectory[i] == '\')
    #else
    if (pcDirectory[i] == '/')
    #endif
    {
    pcDirectory[i + 1] = '';
    break;
    }
    }
    return pcDirectory;
    }

    不为其他,只为快乐!
  • 相关阅读:
    记忆化搜索——luogu滑雪
    如何解决mysql stop fail的问题
    顶级工程师谈机遇、谈跳槽、谈选择
    向周鸿祎学习产品和产品推销方法
    几个概念:x86、x86-64和IA-32、IA-64
    split函数的实现
    谈谈c++中继承中的虚函数
    c++ 编译期与运行期
    读<<大数据时代>>的一些感想
    size_t与size_type的使用
  • 原文地址:https://www.cnblogs.com/1521299249study/p/10178369.html
Copyright © 2011-2022 走看看