Move a file from one directory to another in C++

In this tutorial, we will learn about how to move a file from one directory to another directory using a C++ program. Many a time, we need to manage the files in our directories. So, if you want to move a file using the C++ program you are in the right place. Here you will get the C++ code with output also. To move a file we need to use system function call. So, let’s learn what is a system function call.

System() function call

The system() call executes a system command. So we can use this function in our code to run a system command. The stdlib header file must be included to use this function. The syntax of the system() function is –

int system(const char* system_command);

So, we will use this function to execute the command for moving a file from one directory to another.

Creation of command

Now, the most important task is to create the command which will move a file from one directory to another. The system commands in the Linux system and the Windows system differs.

So we will take the Linux system command to execute our command. In a Linux system, use the command mv to move a file. The command is –

mv source_path destination_path

But in Windows, the command is slightly different. So, if you try to execute system function call in Windows you should use move command instead of mv –

move file_name destination_path

In the program, we have to take the full path of source i.e. path of the file to be moved and the destination path. To create the desired command we will take a string to store the command.

Program to move a file in C++

Now, you have the basic idea of how a file is moved from one directory to another. So, go through the program given below –

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
int main()
{
  char src[50],dest[50],cmd[100];
  cout<<"\nENTER FULL PATH OF FILE TO BE MOVED : ";
  gets(src);
  cout<<"ENTER FULL PATH WHERE FILE IS TO BE MOVED : ";
  gets(dest);
  strcpy(cmd,"mv ");
  strcat(cmd,src);
  strcat(cmd," ");
  strcat(cmd,dest);
  system(cmd);
  return 0;
}

So, this C++ program moves a file specified by the user to the destination directory. In the program, we used 3 strings namely –

  • src[50] To store the path of the file to be moved
  • dest[50] – To store the path where the file is to be moved
  • cmd[100] – To store the Linux command, you can store the Windows command if you want to run this code in Windows operating system

Also, read: Adding a new line to text file in C++

 

Here we used string functions to store the command in a string named cmd. Because we have to pass the command as a parameter to system function. The system function executes the system command and we get the desired results.

After executing this C++ program in Linux system we get the following output –

[email protected]:~$ g++ m.cpp
[email protected]:~$ ./a.out
ENTER FULL PATH OF FILE TO BE MOVED : /home/siddharth/a.txt
ENTER FULL PATH WHERE FILE IS TO BE MOVED : /home/siddharth/Documents
[email protected]:~$

The name of the program is m.cpp. After compilation, we run the executable file named a.out. We give the source and destination path. Let’s see the changes to the file a.txt.

Initially, the a.txt file is in the Home directory. And after executing the program the a.txt file moves to the Documents directory.

So, through this C++ program, we moved a file named a.txt from Home directory to Documents directory successfully. Thank you for reading this article. I hope it helped you and solved your problems.

Leave a Reply

Your email address will not be published. Required fields are marked *