How to Get Current Directory in C++
Last Updated :
17 Jun, 2024
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++.
Get Current Working 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:
Syntax of getcwd()
char *_getcwd(char *buffer, size_t size);
where:
- buffer: is a character array that will be used to store the absolute path of the current working directory.
- size: denotes the maximum length of the buffer array.
C++ Program to get Current Working Directory in Windows Based Systems
The following program illustrates how we can get the current working directory in windows using getcwd function in C++:
C++
// C++ Program to get the current working directory
#include <iostream>
#include <direct.h>
using namespace std;
int main() {
// Define a buffer
const size_t size = 1024;
// Allocate a character array to store the directory path
char buffer[size];
// Call _getcwd to get the current working directory and store it in buffer
if (getcwd(buffer, size) != NULL) {
// print the current working directory
cout << "Current working directory: " << buffer << endl;
}
else {
// If _getcwd returns NULL, print an error message
cerr << "Error getting current working directory" << endl;
}
return 0;
}
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.
Get Current Directory with std::filesystem Method
We can also find the current directory using the new std::filesystem::current_path() method which was introduced in C++17.
C++ Program to get Current Working Directory using Filesystem
The following program illustrates how we can get the current working directory using std::filesystem::current_path() function in C++:
C++
// C++ Program to get the current working directory
#include <filesystem>
#include <iostream>
using namespace std;
int main()
{
// finding and printing the current working directory.
cout << "Current path is " << filesystem::current_path()
<< endl;
return 0;
}
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.
Similar Reads
How to Check a File or Directory Exists in C++? Checking the presence of a directory or a file is one of the most common operations performed by a file system in an Operating System. Most programming languages offer some level of file system accessibility in form of library functions. In this article, you will learn how to test a file or director
4 min read
How to Get File Extension in C++? In C++, we may often find the need to extract the file extension from a given path of the file while working in many applications for processing or validating. In this article, we will learn how to get the file extension in C++. For Example, Input: someFolder â³ filename.ext Output: File Extension =
2 min read
How to Get Environment Variable in C++? Environment variables are globally accessible named values that store information about the system environment where your code is executed. They are also used to store configuration settings, paths to important directories, and other system-specific data as well. In this article, we will learn how t
2 min read
How to Delete a File in C++? C++ file handling allows us to manipulate external files from our C++ program. We can create, remove, and update files using file handling. In this article, we will learn how to remove a file in C++. Delete a File in C++ To remove a file in C++, we can use the remove() function defined inside the
2 min read
Create Directory or Folder with C/C++ Program Problem: Write a C/C++ program to create a folder in a specific directory path. This task can be accomplished by using the mkdir() function. Directories are created with this function. (There is also a shell command mkdir which does the same thing). The mkdir() function creates a new, empty director
2 min read