C++ 获取当前目录

C++ 获取当前目录,第1张

std::filesystem::current_path 还可以改变当前目录

https://en.cppreference.com/w/cpp/filesystem/current_path

直接乎宴使亏拦用getcwd会有告警

https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/getcwd-wgetcwd?view=msvc-160

或销顷胡者使用 get_current_dir_name

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

https://linux.die.net/man/3/get_current_dir_name

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

直接fopen("abc.txt","r")

就可以了。

不过要注意一点,如果你使用VC编译调试,那么在里面点乎岁友!号运行的时候,那个当前目录是指工程目录,而不是Debug文件夹里面。当然,你直接把exe文件拿出来用的岁槐话就是一雀则般意义下的当前目录了。

使用:

int _chdir(

const char *dirname

)

例子:

/梁肆档/ crt_chdir.c

// arguments: C:\WINDOWS

/* This program uses the _chdir function to verify

that a given directory exists. */

#include <direct.h>

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

int main( int argc, char *argv[] )

{

if(_chdir( argv[1] ) )

{

switch (errno)

{

case ENOENT:

printf( "Unable to locate the directory: %s\n", argv[1] )

break

case EINVAL:

printf( "Invalid buffer.\n"雹搜)

break

default:

printf( "Unknown error.\n")

}

}

else

system( "橡乱dir *.exe")

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12320354.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-22
下一篇2023-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存