How to get the current working directory in C ? (std::filesystem)
Jul 09, 2025 am 01:18 AMIn C 17, you can directly get the current working directory using std::filesystem::current_path(). By including the header file
In C 17, std::filesystem
library provides a very convenient way to handle file system paths and operations. Obtaining the current working directory is one of the common requirements and it is not complicated to implement.

Use std::filesystem::current_path()
This is the most direct way:

#include <iostream> #include <filesystem> int main() { std::filesystem::path currentPath = std::filesystem::current_path(); std::cout << "Current working directory: " << currentPath << std::endl; return 0; }
This code will output the current working directory where your program is running. Note that this directory may vary depending on whether you run from the command line, start the IDE, or call it through a script.
Frequently Asked Questions and Precautions
The return type is a path object : you can use
.string()
or.generic_string()
to convert it into string format.Exception handling : By default, an exception will be thrown if an error occurs. If you don't want the program to crash, you can pass in a
std::error_code
parameter to avoid exceptions:std::error_code ec; auto path = std::filesystem::current_path(ec); if (ec) { std::cerr << "Error getting current path: " << ec.message() << std::endl; }
Is it common on different platforms?
Yes, std::filesystem
is cross-platform, and you can use the same code whether you develop on Windows, Linux, or macOS. The path separator under Windows will be automatically used \
(but path
class will help you deal with splicing problems), while the Unix-like system uses /
.
However, the following points should be paid attention to:
- If you splice the path manually, please use
/
or the splicing method ofstd::filesystem::path
to ensure compatibility. - If there are special characters or Chinese in the path string, it is best to make sure that your environment supports UTF-8 encoding, otherwise garbled code may be displayed.
Example: Convert path to string output
If you want to output the path as a string to the log or console, you can do this:
std::cout << "As string: " << currentPath.string() << std::endl; // Or use generic_string() to force the forward slash style std::cout << "Generic: " << currentPath.generic_string() << std::endl;
The results of the output of these two methods may be slightly different, depending on the operating system.
Basically that's it. It is straightforward to use std::filesystem::current_path()
to get the current directory, but don't forget to deal with possible errors and path format conversions.
The above is the detailed content of How to get the current working directory in C ? (std::filesystem). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

How to implement robot control and robot navigation in C++? Robot control and navigation are very important parts of robotics technology. In the C++ programming language, we can use various libraries and frameworks to implement robot control and navigation. This article will introduce how to use C++ to write code examples for controlling robots and implementing navigation functions. 1. Robot control In C++, we can use serial communication or network communication to realize robot control. The following is a sample code that uses serial communication to control robot movement: inclu

In C++ development, null pointer exception is a common error, which often occurs when the pointer is not initialized or is continued to be used after being released. Null pointer exceptions not only cause program crashes, but may also cause security vulnerabilities, so special attention is required. This article will explain how to avoid null pointer exceptions in C++ code. Initializing pointer variables Pointers in C++ must be initialized before use. If not initialized, the pointer will point to a random memory address, which may cause a Null Pointer Exception. To initialize a pointer, point it to an

How to write a simple file encryption program in C++? Introduction: With the development of the Internet and the popularity of smart devices, the importance of protecting personal data and sensitive information has become increasingly important. In order to ensure the security of files, it is often necessary to encrypt them. This article will introduce how to use C++ to write a simple file encryption program to protect your files from unauthorized access. Requirements analysis: Before starting to write a file encryption program, we need to clarify the basic functions and requirements of the program. In this simple program we will use symmetry

How to use the Fibonacci sequence algorithm in C++ The Fibonacci sequence is a very classic sequence, and its definition is that each number is the sum of the previous two numbers. In computer science, using the C++ programming language to implement the Fibonacci sequence algorithm is a basic and important skill. This article will introduce how to use C++ to write the Fibonacci sequence algorithm and provide specific code examples. 1. Recursive method Recursion is a common method of Fibonacci sequence algorithm. In C++, the Fibonacci sequence algorithm can be implemented concisely using recursion. under

How to write a simple music recommendation system in C++? Introduction: Music recommendation system is a research hotspot in modern information technology. It can recommend songs to users based on their music preferences and behavioral habits. This article will introduce how to use C++ to write a simple music recommendation system. 1. Collect user data First, we need to collect user music preference data. Users' preferences for different types of music can be obtained through online surveys, questionnaires, etc. Save data in a text file or database

In C++ programming, processing sound is a very important link. Whether it is an embedded system or a multimedia project, sound needs to be processed and optimized. The following will introduce some sound processing techniques in C++, I hope it can be helpful to everyone. Basic concepts of sound Before understanding sound processing, we need to understand some basic sound concepts. Sound is a mechanical wave caused by objects vibrating in the air. In computers, sound is usually represented as a digital signal. A digital signal is represented by a series of consecutive numbers, where

C performs well in real-time operating system (RTOS) programming, providing efficient execution efficiency and precise time management. 1) C Meet the needs of RTOS through direct operation of hardware resources and efficient memory management. 2) Using object-oriented features, C can design a flexible task scheduling system. 3) C supports efficient interrupt processing, but dynamic memory allocation and exception processing must be avoided to ensure real-time. 4) Template programming and inline functions help in performance optimization. 5) In practical applications, C can be used to implement an efficient logging system.

Efficiently utilize C++ programming skills to build robust embedded system functions. With the continuous development of technology, embedded systems play an increasingly important role in our lives. As a high-level programming language, C++ is flexible and scalable and is widely used in embedded system development. In this article, we will introduce some C++ programming techniques to help developers efficiently use C++ to build robust embedded system functions. 1. Use object-oriented design Object-oriented design is one of the core features of the C++ language. In the embedded system
