美文网首页
C++ 获取当前目录

C++ 获取当前目录

作者: book_02 | 来源:发表于2021-07-06 21:16 被阅读0次

1. 说明

  1. 如果编译器支持C++17,则建议使用std::filesystem::current_path
  2. 如果只在windows平台使用,可使用_getcwd
  3. 如果只在linux平台使用,可使用getcwd
  4. 如果代码要跨平台使用,可以使用通过编译统一的版本

2. C++17 std::filesystem::current_path

#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
    std::cout << "Current working directory: " << fs::current_path() << '\n'; 
}
Current working directory:  "D:/local/ConsoleApplication1"

std::filesystem::current_path 还可以改变当前目录
https://en.cppreference.com/w/cpp/filesystem/current_path

3. windows平台 _getcwd

#include <direct.h>

std::string current_working_directory()
{
    char buff[250];
    _getcwd(buff, 250); 
    std::string current_working_directory(buff);
    return current_working_directory;
}

int main()
{
    std::cout << "Current working directory: " << current_working_directory() << endl;
}

直接使用getcwd会有告警
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/getcwd-wgetcwd?view=msvc-160

4. linux平台 getcwd

#include <unistd.h>

int main() 
{
    char buff[250];
    getcwd(buff, 250);
    string current_working_directory(buff);
    cout << "Current working directory: " << current_working_directory<< endl;

    return 0;
}

或者使用get_current_dir_name

#include <unistd.h>

int main() 
{
    char *cwd = get_current_dir_name();
    cout << "Current working directory: " << cwd << endl;
    free(cwd);

    return 0;
}

https://man7.org/linux/man-pages/man3/getcwd.3.html
https://linux.die.net/man/3/get_current_dir_name

5. 通过编译宏统一两者版本

参考: https://www.tutorialspoint.com/find-out-the-current-working-directory-in-c-cplusplus

#if defined(_MSC_VER)
    #include <direct.h>
    #define GetCurrentDir _getcwd
#elif defined(__unix__)
    #include <unistd.h>
    #define GetCurrentDir getcwd
#else
#endif

std::string get_current_directory() 
{
    char buff[250]; 
    GetCurrentDir(buff, 250);
    string current_working_directory(buff);
    return current_working_directory;
}

int main(int argc, char* argv[])
{
    std::cout << "Current working directory: " << get_current_directory() << endl;
    
    return 0;
}

相关文章

  • C++ 获取当前目录

    1. 说明 如果编译器支持C++17,则建议使用std::filesystem::current_path 如果只...

  • 【Java】【web】获取访问项目路径

    获取访问根地址 获取访问项目跟目录 获取当前class文件目录 获取当前访问脚本目录 获取当前访问全地址 base...

  • 目录管理操作

    目录管理基本操作有添加、移动、删除、获取当前工作目录、获取当前目录的子目录等

  • os 包

    name 获取当前平台类型 getcwd() 获取当前工作路径 listdir() 获取当前目录列表 system...

  • 获取当前信息

    目录:1、获取当前设备类型2、获取当前语言环境3、获取当前APP版本号 1、获取当前设备类型 2、获取当前语言环境...

  • Android-获取当前应用根目录

    一:获取 Android 当前应用内部私有根目录 二:获取 Android 当前应用外部公共根目录

  • 记录

    __DIR__ //当前文件的父目录 dirname(); //获取父目录 __FILE__ //获取当前文件的地...

  • C++工具函数

    C++ 工具函数 读取二进制文件 字符串split 删除文件 定义枚举类,并将枚举常量转成int 获取当前目录剩余...

  • getcwd()函数:取得当前的工作目录(working dir

    用 c/c++ 获取当前工作目录的方法:getcwd首先我们来看一下该函数的声明: 用法介绍:参数说明:getcw...

  • python的os、glob模块

    os.getcwd():获取当前工作目录os.listdir():获取指定目录内容glob.glob():获取指定...

网友评论

      本文标题:C++ 获取当前目录

      本文链接:https://www.haomeiwen.com/subject/aycnultx.html