C++C++17Introduction to C++Learn C++

This Is How To Use File Operations In C++

C++ is a great programming language that you can reach every part of your device or operating system. File system of an operating system is very important and you must know well both File System features of that Operating System and features of your programming language. Thus, we can copy, delete and get file information.

In this post, you’ll learn how to use DOS commands to operate on files, as well as how to use system commands in C++ to list, copy, rename, delete, and move files and set file permissions using system commands in C++. By learning how to use file operations in c++, it will help you to easily build C++ applications using the C++ App.

Using System Commands

On Windows and some other operating systems you can use System() command to use System Commands like cd, mkdir rmdir commands etc. For example we can create a folder by using mkdir command and we can copy folder to another folder by using xcopy command and we can remove a folder by using rmdir command. See example below,

How to list files in a directory or folder in C++

In C++, Std library has great features, classes and methods, and there is a directory_iterator(), we can use this to get all file names in that path.

We can use this directory_iterator() with for() loop as in given example below,

We can use recursive_directory_iterator() to list all files in that folder.

How to check if a file exists with C++

We can use filesystem::exists(path+filename) to check a file if it exists in a file system.

How to copy a file in C++

How do I set or change file permissions with C++?

Creating a file with specific permissions

How to rename a file in C++

How to remove or delete a file with C++


You can download and try RAD Studio C++ Builder for free. Why not try it today?

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?