O'Reilly logo

Just Enough C/C++ Programming by Guy W. Lecky-Thompson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Renaming and Deleting Files

If the program needs to rename a file (to create a backup, for example, before writing the new file), it can be done with a simple call to the rename function from stdio.h:

int rename ( const char * old_name, const char * new_name )

If a file with the new_name already exists, the return value will be an error. To check in advance, the program simply needs to call the fopen function with the r flag set. The same applies to the file referred to with the old_name parameter.

You can delete a file by calling the remove function:

int remove ( const char * filename )

Some compilers also provide an unlink function, which performs the same file deletion. File deletion will take place if the file exists and if it’s empty. Some ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required