int remove(const char* filename);
remove()
function takes a single argument filename and returns an integer value. It deletes the file pointed by the parameter. Incase the file to be deleted is opened by a process, the behaviour of remove()
function is implementation-defined.
It is defined in <cstdio> header file.
The remove() function returns:
1) Zero if the file is successfully deleted.
2) Non zero if error occurs.
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
char filename[] = "C:Usersile.txt";
/* Deletes the file if exists */
if (remove(filename) != 0)
perror("File deletion failed");
else
cout << "File deleted successfully";
return 0;
}
When you run the program, the output will be:
If the file is deleted successfully:
File deleted successfully
If the file is not present:
File deletion failed: No such file or directory
Name | Views | Likes |
---|---|---|
C++ ftell() Function | 254 | 0 |
C++ fseek() Function | 479 | 0 |
C++ rewind() Function | 383 | 0 |
C++ vsprintf() Function | 199 | 0 |
C++ fgetc() Function | 286 | 0 |
C++ vsscanf() Function | 197 | 0 |
C++ putc() Function | 316 | 0 |
C++ fputc() Function | 265 | 0 |
C++ fread() Function | 237 | 0 |
C++ vsnprintf() Function | 381 | 0 |
C++ getc() Funcion | 228 | 0 |
C++ |
244 | 0 |
C++ fwrite() Function | 259 | 0 |
C++ fsetpos() Function | 200 | 0 |
C++ getchar() function | 475 | 0 |
C++ tmpnam() Function | 206 | 0 |
C++ ungetc() Function | 229 | 0 |
C++ fgetpos() Function | 205 | 0 |
C++ setbuf() Function | 189 | 0 |
C++ fputs() Function | 238 | 0 |
C++ fclose() Function | 282 | 0 |
C++ Clearerr() Function | 231 | 0 |
C++ fgets() Function | 258 | 0 |
C++ Remove() Function | 252 | 0 |
Comments