Rename file in C++

Posted on 2004-11-01
Last Modified: 2012-05-05
I am trying to rename a file with the following code and I get a

error renaming file:  No Such File or Directory

This oldname variable does exist

any thoughts?

#include <stdio.h>

int main ()
  int result;
  char oldname[] ="C:\cplus\trythis.txt";
  char newname[] ="C:\cplus\newfolder\newname.txt";
  result= rename( oldname , newname );
  if (result != 0 )
    perror( "Error renaming file" );
    cout << oldname;
  return 0;


Question by:jmsloan
    LVL 16

    Expert Comment

    What about "newfolder"? Does that folder exist?
    LVL 3

    Author Comment

    yes it does.  I'm on xp does that matter.
    LVL 86

    Accepted Solution

    Also, you need to use double backslashes in path names:

      int result;
     char oldname[] ="C:\\cplus\\trythis.txt";
     char newname[] ="C:\cplus\\newfolder\\newname.txt";
     result= rename( oldname , newname );
    LVL 3

    Author Comment

    thats what it was,


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Learn The Basics of Ethical Hacking & Pen Testing

    Computer and network security is one of the fastest growing and most essential industries in technology, meaning companies will pay big bucks for ethical hackers. This is the perfect course to leap into this lucrative career, learning how to use ethical hacking to reveal ...

    C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
    This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now