Rename () function in C

The function int rename (const char * old_filename, const char * new_filename) in Library C standard makes filename referenced, changed from old_filename to new_filename.

The function int rename (const char * old_filename, const char * new_filename) in Library C standard makes filename referenced, changed from old_filename to new_filename.

Declare rename () function in C

Below is the declaration for rename () function in C:

 int rename ( const char * old_filename , const char * new_filename ) 

Parameters

old_filename - This is the string containing the file name to be renamed and / or moved.

new_filename - This is the string containing the new name for the file.

Returns the value

If successful, the function returns 0. If there is an error, the function returns -1 and errno is set appropriately.

For example

The following program C illustrates how to use the rename () function in C:

 #include int main () { int ret ; char oldname [] = "baitapc.txt" ; char newname [] = "baitapc1.txt" ; ret = rename ( oldname , newname ); if ( ret == 0 ) { printf ( "Doi ten file thanh cong !!!" ); } else { printf ( "Error: khong the doi ten file" ); } return ( 0 ); } 

Suppose we have baitapc.txt with some content. We are preparing to delete this file by using the above program. Compile and run the above program to create the following message and the file will be renamed to baitapc1.txt.

Rename () function in C Picture 1Rename () function in C Picture 1

According to Tutorialspoint

Previous post: remove () function in C

Next lesson: The rewind () function in C

4 ★ | 1 Vote