
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find 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/C++
In C/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
Following is the syntax of the CWD using getcwd() function in C/C++:
#include <unistd.h> char *getcwd(char *buf, size_t size);
Example 1
In this example, we ask the operating system for the current working directory using the getcwd() function, then it prints successful or display error if it fails.
#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; }
Output
Current working directory: /home/user/project or prints error if getcwd() fails.
Prints error if getcwd() fails.
Example 2
In this example, we use the getcwd() function to retrieve the current working directory. It stores the directory path in the cwd array. Then it prints the output successful, if it fails returns error.
#include <iostream> #include <unistd.h> using namespace std; int main() { char cwd[1024]; if (getcwd(cwd, sizeof(cwd)) != NULL) { cout << "Current working directory: " << cwd << endl; } else { perror("getcwd() error"); } return 0; }
Output
The above code produces the following result:
Current working directory: /home/user/cppproject or prints error if getcwd() fails.
Using filesystem::current_path() in C++17
In C++, we can achieve the same using the <filesystem> library introduced in C++17. It provides an elegant and modern way to get the Current working directory.
Syntax
Following is the syntax of the CWD using filesystem::current_path() in C++17:
#include <filesystem> filesystem::current_path();
Example
In this example, we call a function to ask the operating system, "What folder am I in?" and the OS replies with the directory path. It is like figuring out your current location before you start navigating.
#include <iostream> #include <filesystem> namespace fs = std::filesystem; int main() { std::cout << "Current path is: " << fs::current_path() << std::endl; return 0; }
Output
The above program generates the following output:
Current path is: "/home/user/cpp17"