Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
C Library
cassert (assert.h)
cctype (ctype.h)
cerrno (errno.h)
cfloat (float.h)
ciso646 (iso646.h)
climits (limits.h)
clocale (locale.h)
cmath (math.h)
csetjmp (setjmp.h)
csignal (signal.h)
cstdarg (stdarg.h)
cstddef (stddef.h)
cstdio (stdio.h)
cstdlib (stdlib.h)
cstring (string.h)
ctime (time.h)
cstdio (stdio.h)
functions:
clearerr
fclose
feof
ferror
fflush
fgetc
fgetpos
fgets
fopen
fprintf
fputc
fputs
fread
freopen
fscanf
fseek
fsetpos
ftell
fwrite
getc
getchar
gets
perror
printf
putc
putchar
puts
remove
rename
rewind
scanf
setbuf
setvbuf
sprintf
sscanf
tmpfile
tmpnam
ungetc
vfprintf
vprintf
vsprintf
macro constants:
EOF
FILENAME_MAX
NULL
TMP_MAX
objects:
stderr
stdin
stdout
types:
FILE
fpos_t
size_t


rename

function
<cstdio>
int rename ( const char * oldname, const char * newname );

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 returned 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* 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