![]() |
VOOZH | about |
The current working directory, also known as the present working directory, is the location on the file system where the executing program is located and operates from. When working with files and directories in C++, it is important to determine the current working directory to find the resource files. In this article, we will learn how to get the current directory in C++.
To get the current working directory in C++, we can use the getcwd() function provided by the <direct.h>header. Below is the syntax to use the getcwd function:
char *_getcwd(char *buffer, size_t size);
where:
The following program illustrates how we can get the current working directory in windows using getcwd function in C++:
Output
Current working directory: C:\Users\Desktop
Time Complexity: O(1)
Auxiliary Space: O(N), here N denotes the maximum size of the buffer array.
We can also find the current directory using the new std::filesystem::current_path() method which was introduced in C++17.
The following program illustrates how we can get the current working directory using std::filesystem::current_path() function in C++:
Output
Current working directory: C:\Users\Desktop
Time Complexity: O(1)
Auxiliary Space: O(N), here N denotes the maximum size of the buffer array.