Thread: Problem with rename function

  1. #1
    Registered User
    Join Date
    Nov 2006

    Red face Problem with rename function


    I am trying to rename a file, like so:

    rename("C:\\me\\example.txt", "C:\\me\\real_thing.ABD");
    I have specified the directory because I am still running in debug mode. However, the renaming is not working for some reason.

    These file names refer to:
    fstream fexample;
    ofstream freal;
    Prior to renaming. freal was cleared, closed, removed. Like so,
    freal.close(); freal.clear();
    rename("C:\\me\\example.txt", "C:\\me\\real_thing.ABD");
    Any help/ideas?


  2. #2
    Registered User
    Join Date
    Nov 2006
    aHa! I realised the problem.

    I must first close both open files in order to rename.

    So, when I close the file for which I am renaming I want to continue writing to this file. Therefore, I will open it again after this. However, how can I get it to append any further data to the end of the file rather than overwriting what is already there?


  3. #3
    Registered User
    Join Date
    Nov 2006
    ios::app ofcourse.

    Sorry guys!

    Maybe the close the file issue might help someone as silly as me in the future!!


  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    rename - change the name or location of a file

    #include <stdio.h>

    int rename(const char *oldpath, const char *newpath);

    rename() renames a file, moving it between directories if required. Any other hard links to the file (as created using link(2)) are unaffected.
    Open file descriptors for oldpath are also unaffected.

    If newpath already exists it will be atomically replaced (subject to a few conditions; see ERRORS below), so that there is no point at which another
    process attempting to access newpath will find it missing.

    If oldpath and newpath are existing hard links referring to the same file, then rename() does nothing, and returns a success status.

    If newpath exists but the operation fails for some reason rename() guarantees to leave an instance of newpath in place.

    oldpath can specify a directory. In this case, newpath must either not exist, or it must specify an empty directory.

    However, when overwriting there will probably be a window in which both oldpath and newpath refer to the file being renamed.

    If oldpath refers to a symbolic link the link is renamed; if newpath refers to a symbolic link the link will be overwritten.

    On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
    Perhaps look at the return result, and print the appropriate error message?

    As opposed to not checking anything and having nothing better to offer than "it doesn't work"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 01-26-2009, 08:01 PM
  2. renaming file without rename() function?
    By subodh_dg in forum C Programming
    Replies: 3
    Last Post: 03-26-2005, 09:40 AM
  3. Rename file problem
    By Emporio in forum C Programming
    Replies: 2
    Last Post: 06-05-2002, 09:36 AM
  4. rename() problem
    By Dimeslime in forum C Programming
    Replies: 4
    Last Post: 02-09-2002, 05:38 PM
  5. Problem with cgi script - can't rename files
    By bjdea1 in forum C Programming
    Replies: 2
    Last Post: 12-12-2001, 04:09 PM