The remove() function in C++ deletes a specified file. It is defined in the cstdio header file.
Example
remove() Syntax
The syntax of the remove() function is:
remove(const char* filename);
remove() Parameters
The remove() function takes the following parameter:
- filename - pointer to the C-string containing the name of the file along with the path to delete
Note: Variables of the C++ string class cannot be used as parameters for remove().
remove() Return Value
The remove() function returns:
- zero if the file is successfully deleted
- non-zero if error occurs in deletion process
remove() Prototype
The prototype of remove() as defined in the cstdio header file is:
int remove(const char* filename);
Delete Opened Files with remove()
In case the file to be deleted is opened by a process, the behaviour of remove() function is implementation-defined:
- POSIX systems - If the name was the last link to a file, but any processes still have the file open, the file will remain in existence until the last running process closes the file.
- Windows - The file won't be allowed to be deleted if it remains open by any process.
Example: C++ remove()
Output
File deletion failed