Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find out the current working directory in C/C++
To find the Current Working Directory (CWD) in C or C++ is like asking your program: "Hey, where am I right now?". Simply we can say that it is like a folder of your program which is present and used to operate in.
We can use functions like getcwd() from unistd.h in C/C++ or filesystem::current_path() from C++17. Below are the list of the ways to achieve this −
Using getcwd() Function in C
In C, we use the getcwd() function. This function gets the path to the current working directory. You can provide a buffer (a space in memory to store the result) and its size, and the function fills it with the directory path.
Syntax
#include <unistd.h> char *getcwd(char *buf, size_t size);
Example 1: Basic Usage of getcwd()
In this example, we ask the operating system for the current working directory using the getcwd() function −
Note: On Windows, you may need to use_getcwd()from<direct.h>instead ofgetcwd().
#include <stdio.h>
#include <unistd.h>
int main() {
char cwd[1024]; /* Buffer to store the directory path */
if (getcwd(cwd, sizeof(cwd)) != NULL) {
printf("Current working directory: %s\n", cwd);
} else {
perror("getcwd() error"); /* Prints error if getcwd() fails */
}
return 0;
}
Current working directory: /home/user/project
Example 2: Using NULL Buffer for Dynamic Allocation
When you pass NULL as the buffer, getcwd() allocates memory automatically −
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
char *cwd = getcwd(NULL, 0); /* Automatic memory allocation */
if (cwd != NULL) {
printf("Current working directory: %s\n", cwd);
free(cwd); /* Free the allocated memory */
} else {
perror("getcwd() error");
}
return 0;
}
Current working directory: /home/user/project
Using filesystem::current_path() in C++17
In C++17, we can achieve the same using the <filesystem> library. It provides an elegant and modern way to get the current working directory.
Syntax
#include <filesystem> std::filesystem::current_path();
Example
In this example, we use the modern C++17 approach to get the current working directory −
Note: You may need to compile with-std=c++17flag and link with-lstdc++fson some compilers.
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main() {
std::cout << "Current path is: " << fs::current_path() << std::endl;
return 0;
}
Current path is: "/home/user/cpp17"
Comparison
| Method | Language | Pros | Cons |
|---|---|---|---|
getcwd() |
C/C++ | Portable, handles manual memory | Manual buffer management |
filesystem::current_path() |
C++17+ | Modern, automatic memory management | Requires C++17 or later |
Conclusion
The getcwd() function is the standard C approach for getting the current working directory, while std::filesystem::current_path() offers a modern C++17 alternative. Both methods are effective for determining your program's current location in the filesystem.
