Problem with rename function

This is a discussion on Problem with rename function within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to rename a file, like so: Code: rename("C:\me\example.txt", "C:\me\real_thing.ABD"); I have specified the directory because I ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Red face Problem with rename function

    Hi,

    I am trying to rename a file, like so:

    Code:
    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:
    Code:
    fstream fexample;
    ofstream freal;
    Prior to renaming. freal was cleared, closed, removed. Like so,
    Code:
    freal.close(); freal.clear();
    remove("C:\\me\\real_thing.ABD");
    
    rename("C:\\me\\example.txt", "C:\\me\\real_thing.ABD");
    Any help/ideas?

    Thanks!

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    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?

    Cheers

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

    Sorry guys!

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

    Cheers

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    NAME
    rename - change the name or location of a file

    SYNOPSIS
    #include <stdio.h>

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

    DESCRIPTION
    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.

    RETURN VALUE
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 01-26-2009, 07:01 PM
  2. renaming file without rename() function?
    By subodh_dg in forum C Programming
    Replies: 3
    Last Post: 03-26-2005, 08: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, 04:38 PM
  5. Problem with cgi script - can't rename files
    By bjdea1 in forum C Programming
    Replies: 2
    Last Post: 12-12-2001, 03:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21