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.
Table of Contents
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,
1 2 3 4 5 6 |
#include <iostream> int main() { std::system("xcopy D:\\file1.txt D:\\file2.txt"); } |
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.
1 |
std::filesystem::directory_iterator(path) |
We can use this directory_iterator() with for() loop as in given example below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <filesystem> #include <string> int main() { std::string path = "C://Windows"; for (const auto & entry : std::filesystem::directory_iterator(path)) { std::cout << entry.path() << std::endl; } getchar(); return 0; } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> #include <filesystem> int main() { if( std::filesystem::exists("D:\\test1.txt")) { cout << "file exists\n"; } getchar(); return 0; } |
How to copy a file in C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <filesystem> int main() { const auto copy_options = std::filesystem::copy_options::update_existing | std::filesystem::copy_options::recursive | std::filesystem::copy_options::fileses_only; std::filesystem::copy_file( "learncplusplus1.txt", "learncplusplus2.txt", copy_options ); getchar(); return 0; } |
How do I set or change file permissions with C++?
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> #include <filesystem> int main() { std::filesystem::permissions("D:\\test.txt", std::filesystem::perms::owner_all | std::filesystem::perms::group_all, std::filesystem::perm_options::add); getchar(); return 0; } |
Creating a file with specific permissions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <filesystem> #include <fstream> #include <bitset> int main() { std::ofstream("D:\\mytesting.txt"); // create file fs::permissions("test.txt", fs::perms::owner_all | fs::perms::group_all, fs::perm_options::add); getchar(); return 0; } |
How to rename a file in C++
1 2 3 4 5 6 7 8 9 10 |
#include <iostream> #include <filesystem> int main() { std::filesystem::rename("file1.txt", "file2.txt"); getchar(); return 0; } |
How to remove or delete a file with C++
1 2 3 4 5 6 7 8 9 10 |
#include <iostream> #include <filesystem> int main() { std::filesystem::remove("D:\\file1.txt"); getchar(); return 0; } |
You can download and try RAD Studio C++ Builder for free. Why not try it today?
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition