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

This Is How To Use File Operations In C++

Do you want to use dos commands to operate on files? Do you know that you can use system commands in C++ to operate on files ? How can we list, copy, rename, delete, move files ? Can we set permission of files by using system commands in C++? We will explain all the answers of these questions below.

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.

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

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++Introduction to C++Language FeatureLearn C++

How To Generate A Random File Name on Windows

C++Introduction to C++Language FeatureLearn C++

How To Include Or Exclude Trailing Backslashes On Windows

C++Introduction to C++Language FeatureLearn C++

How To Check Characters of File Names and Paths on Windows

C++Introduction to C++Language FeatureLearn C++

How to use SameFileName method on Windows