如何使用C语言getcwd函数精确获取当前项目运行路径?
- 内容介绍
- 文章标签
- 相关推荐
本文共计874个文字,预计阅读时间需要4分钟。
头文件:在Unix下是unistd.h,在Windows下是direct.h代码:在Unix下使用unistd.h,在Windows下使用direct.h
头文件:
在unix下是unistd.h,VS下是direct.h
代码:
#include <stdio.h> #include <string> // 区分此函数是在Windows环境调用还是Linux环境调用 #if defined (_WIN64) || defined (WIN32) || defined (_WIN32) //printf("---Windows---\n"); #include <direct.h> #else //printf("---Linux---\n"); #include <unistd.h> #endif /****************************************************************************** * * 功能: * 获得当前程序的工作路径(绝对路径),即运行路径! * * 注意: * 头文件在unix下是unistd.h,VS下是direct.h,应该依编程者的环境而定. * 这里解释一下运行路径,即是程序开始运行的路径,例如: * 1.如果是在Windows环境的VS编译器中运行项目,则返回的是项目路径, * 即代码文件路径(.h和.cpp路径),因为是在编译器中运行的项目,所以 * 程序的运行路径也是才项目路径中开始运行的。
本文共计874个文字,预计阅读时间需要4分钟。
头文件:在Unix下是unistd.h,在Windows下是direct.h代码:在Unix下使用unistd.h,在Windows下使用direct.h
头文件:
在unix下是unistd.h,VS下是direct.h
代码:
#include <stdio.h> #include <string> // 区分此函数是在Windows环境调用还是Linux环境调用 #if defined (_WIN64) || defined (WIN32) || defined (_WIN32) //printf("---Windows---\n"); #include <direct.h> #else //printf("---Linux---\n"); #include <unistd.h> #endif /****************************************************************************** * * 功能: * 获得当前程序的工作路径(绝对路径),即运行路径! * * 注意: * 头文件在unix下是unistd.h,VS下是direct.h,应该依编程者的环境而定. * 这里解释一下运行路径,即是程序开始运行的路径,例如: * 1.如果是在Windows环境的VS编译器中运行项目,则返回的是项目路径, * 即代码文件路径(.h和.cpp路径),因为是在编译器中运行的项目,所以 * 程序的运行路径也是才项目路径中开始运行的。

