Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Rename file in C++

Posted on 2004-11-01
4
Medium Priority
?
1,431 Views
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;
}


Thanks,

jmsloan
0
Comment
Question by:jmsloan
  • 2
4 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 12464847
What about "newfolder"? Does that folder exist?
0
 
LVL 3

Author Comment

by:jmsloan
ID: 12464897
yes it does.  I'm on xp does that matter.
0
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 12464984
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 );
0
 
LVL 3

Author Comment

by:jmsloan
ID: 12465053
thats what it was,

Thanks
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

916 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