. Advertisement .
..3..
. Advertisement .
..4..
There are several ways to get current directory in C++, depending on which platform you are using. Here is a short explanation of how they work.
Get Current Directory in C++
std::filesystem::current_path
The function current_path()
belongs to the Filesystem library, which has been available since C++17. It comes with several operations that can help you deal with file systems and related components like files, folders, and paths. You can also obtain support for this function by using Boost – a popular set of C++ libraries.
Technically, facilities like the function current_path()
might not be available if your system couldn’t provide the required capabilities or inaccessible to the library, this is rarely the case. It should work most of the time, regardless of your platform, as long as your compiler supports C++17. Only remember that if you have a legacy file system like FAT, some features like multiple hard links or symlinks aren’t supported.
When your system is compatible with the Filesystem library, you can use the function current_path() to return (or even change) the current directory simply like this:
#include <filesystem>
path current_path();
It is important to note that this function only returns the absolute path of the working directory associated with the C++ process. This directory is what C++ uses as the starting point when it resolves relative paths.
Here is an example of using std::filesystem::current_path
in a Linux system:
#include <iostream>
#include <filesystem>
int main()
{
std::cout << "Current directory is " << std::filesystem::current_path() << '\n'; // (1)
}
Output:
Current path is "/home/ITTutoria/Articles"
getcwd()
The header file unistd.h provides access to many operation system APIs on POSIX platforms.
It has been available in both C and C++ for a long time, making it a mature and widely supported alternative to std::filesystem::current_path
when you develop C++ programs on Unix-like systems like Linux or macOS. Most popular compilers like LLVM and GCC also support it.
Understanding how this getcwd()
works is a requirement when you want to become an embedded software engineer, for instance. If you use Windows but have compatibility layers like MinGW or Cygwin installed, this header is available as well. Those layers offer their own implementations of unistd.h
.
Here is an example of using getcwd()
from this header:
#include <iostream>
#include <unistd.h>
using namespace std;
string current_dir() {
char buff[FILENAME_MAX];
getcwd( buff, FILENAME_MAX );
string current_working_dir(buff);
return current_working_dir;
}
int main() {
cout << "Current directory is " << current_dir() << '\n';
}
Output:
Current directory is /home/ITTutoria/Articles
The function getcwd()
copies the absolute path of the current directory into the array buff of the length FILENAME_MAX. If your current working directory has a name longer than this size, the function will return NULL and set errno to ENAMETOOLONG.
Keep in mind that it is different from getwd()
, which doesn’t use malloc()
to allocate any memory. Some implementations of getcwd()
, such as glibc’s, use malloc()
to create the buffer buff dynamically if it is NULL.
_getwd()
The header direct.h is provided by Windows. Like unistd.h
on POSIX systems, it comes with functions for file system manipulations, including getting the path of the current working directory.
You can change the code above slightly to make it work on Windows:
#include <iostream>
#include <direct.h>
#define getcwd _getcwd
using namespace std;
string current_dir() {
char buff[FILENAME_MAX];
getcwd( buff, FILENAME_MAX );
string current_working_dir(buff);
return current_working_dir;
}
int main() {
cout << "Current directory is " << current_dir() << '\n';
}
Conclusion
You can get current directory in C++ with many functions. Some of them should be available in all operating systems, while others are platform-specific. But all of them can deal with the underlying file system and provide operations like returning the path of the current working directory. If you want to create a directory to try out the functions above, check out this guide.
Leave a comment