title: How to move file in Linux using C++
body: <p>How do you move a file from one location to another using a C++ program in Linux?
  I've written a program to do this and it runs, but when I try to move a file to a different directory it doesn't move the file, I get the error message from the cout statement. When I try to just rename the file, moving it to the same directory with a new name it works. How can I fix my code so it will be able to move files to another directory?</p>
  
  <p>Here's the code I've written:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;stdio.h&gt;
  using namespace std;
  
  int main ()
  {
    int result=1;
    char oldname[500];
    char newname[500];
    cout &lt;&lt; "Enter the name of a file you want to move (include directory structure)";
    cin &gt;&gt; oldname;
    cout &lt;&lt; "Enter the new location (include directory structure)";
    cin &gt;&gt; newname;
  
    result = rename( oldname , newname );
    if ( result == 0 )
      cout &lt;&lt; "File successfully moved" &lt;&lt; endl;
    else
      cout &lt;&lt; "Error moving file" &lt;&lt; endl;
    return 0;
  }
  </code></pre>
  
  <p><strong>Edit:</strong>
  I added perror to my code and the error message displayed is "Error moving file: No such file or directory" even though the directory I tried moving it to does exist and it has create and delete files permissions.</p>