c++中可以重新命名文件吗
比如e.txt命名为e.hrt
rename function
int rename ( const char * oldname, const char * newname ); <cstdio>
Rename file
Changes the name of the file or directory specified by oldname to newname.
If oldname and newname specify different paths and this is supported by the system, the file is moved to the new location.
This is an operation performed directly on a file; No streams are involved in the operation.
Parameters
oldname
C string containing the name of the file to be renamed and/or moved. This file must exist and the correct writing permissions should be available.
newname
C string containing the new name for the file. This shall not be the name of an existing file; if it is, the behavior to be expected depends on the running environment, which may either be failure or overriding.
Return value
If the file is successfully renamed, a zero value is returned.
On failure, a nonzero value is reurned and the errno variable is set to the corresponding error code. Error codes are numerical values representing the type of failure occurred. A string interpreting this value can be printed to the standard error stream by a call to perror.
Example
/* rename example */
#include <stdio.h>
int main ()
{
int result;
char oldname[] ="oldname.txt";
char newname[] ="newname.txt";
result= rename( oldname , newname );
if ( result == 0 )
puts ( "File successfully renamed" );
else
perror( "Error renaming file" );
return 0;
}
If the file oldname.txt could be succesfully renamed to newname.txt the following message would be written to stdout:
File successfully renamed
Otherwise, a message similar to this will be written to stderr:
Error renaming file: Permission denied
See also
remove Remove file (function)