I know you can use _unlink() to delete a file. But is there anyway to rename a file in c++?
I know you can use _unlink() to delete a file. But is there anyway to rename a file in c++?
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
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++.
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
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.
i already posted it.
"only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein
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 );
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 );
hmmm...that didn't work either.
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.
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 );
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.