renaming a file in c++

This is a discussion on renaming a file in c++ within the C++ Programming forums, part of the General Programming Boards category; I know you can use _unlink() to delete a file. But is there anyway to rename a file in c++?...

  1. #1
    Unregistered
    Guest

    Post renaming a file in c++

    I know you can use _unlink() to delete a file. But is there anyway to rename a file in c++?

  2. #2
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Why not read in the file, write it to a new file, then delete the old file?
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  3. #3
    Unregistered
    Guest
    hmmm, but i need the new file to have the same filename as the old file....

    This is what i'm doing...

    i'm opening a file, lets call its test.cpp, writing to a temp file called temp.cpp. I will then delete test.cpp, and need to automatically rename temp.cpp to test.cpp....but i don't know how to rename in c++.

  4. #4
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Ok if on a Windows system, or DOS under windows: (straight from MSDN)
    rename, _wrename
    Rename a file or directory.

    int rename( const char *oldname, const char *newname );

    int _wrename( const wchar_t *oldname, const wchar_t *newname );

    Routine Required Header Compatibility
    rename <io.h> or <stdio.h> ANSI, Win 95, Win NT
    _wrename <stdio.h> or <wchar.h> Win NT


    For additional compatibility information, see Compatibility in the Introduction.

    Libraries

    LIBC.LIB Single thread static library, retail version
    LIBCMT.LIB Multithread static library, retail version
    MSVCRT.LIB Import library for MSVCRT.DLL, retail version


    Return Value

    Each of these functions returns 0 if it is successful. On an error, the function returns a nonzero value and sets errno to one of the following values:

    EACCES

    File or directory specified by newname already exists or could not be created (invalid path); or oldname is a directory and newname specifies a different path.

    ENOENT

    File or path specified by oldname not found.

    EINVAL

    Name contains invalid characters.

    For other possible return values, see _doserrno, _errno, syserrlist, and _sys_nerr.


    Parameters

    oldname

    Pointer to old name

    newname

    Pointer to new name

    Remarks

    The rename function renames the file or directory specified by oldname to the name given by newname. The old name must be the path of an existing file or directory. The new name must not be the name of an existing file or directory. You can use rename to move a file from one directory or device to another by giving a different path in the newname argument. However, you cannot use rename to move a directory. Directories can be renamed, but not moved.

    _wrename is a wide-character version of _rename; the arguments to _wrename are wide-character strings. _wrename and _rename behave identically otherwise.

    Generic-Text Routine Mappings

    TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
    _trename rename rename _wrename
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  5. #5
    Unregistered
    Guest
    ahh, i know what u mean now...

    when temp.cpp is done, i should delete test.cpp, and create a new test.cpp to which i copy temp.cpp....then i can delete temp.cpp.....but i prefer to rename temp.cpp to test.cpp since i'm dealing with so many files and i've already done so much writing to get temp.cpp.

    I know that sounds confusing... but basically is there a way to rename a file without writing to a new file and deleting the old file.

  6. #6
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    i already posted it.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  7. #7
    Unregistered
    Guest
    Can u help me implement it? I'm having probs...this is my interpretation..


    const char *tempfile = "temp.cpp";
    const char *newname = "final.cpp";

    rename( tempfile, newname );

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Maybe you need the full path. For example:

    const char *tempfile = "\\dir\\subdir\\temp.cpp";
    const char *newname = "\\dir\\subdir\\final.cpp";

    rename( tempfile, newname );

  9. #9
    Unregistered
    Guest

    Unhappy

    hmmm...that didn't work either.

  10. #10
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212
    the function rename() I think has the prototype...

    int rename(char *oldname, char *newname);

    This function operates from the current directory. So it will rename the files in the current directory if they are valid. For example,

    rename("temp.cpp", "test.cpp");

    will work if you are running the program in the directory which contains the file "temp.cpp". Im not sure how to do it in a separate directory.
    Last edited by kwigibo; 04-12-2002 at 12:48 AM.

  11. #11
    Unregistered
    Guest
    yea, all files i'm working with all in the same directory. COuld it have anything to do wit the one i open temp.cpp in the file?

    This is how i opened it...

    const char *tempfile = "temp.cpp";
    ofstream outFile;
    outFile.open( tempfile );

  12. #12
    Unregistered
    Guest
    alrighty, i got it to work...seems i had to close the ofstream connected to temp.cpp first...thanks for all the help, especially xds4lx.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  2. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  3. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 09:57 AM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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