How to delete a file in C

2 Answers

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
     
int main(int argc, char **argv) 
{ 
    //  int unlink(const char *pathname) deletes a name from the filesystem
     
    if (unlink("d:\\data.bin") == -1) {
        puts("Error delete file");
        exit(1);
    }
 
    puts("File deleted");
      
    return 0;
}
     
     
     
     
/*
    
run:
     
File deleted
  
*/

 



answered Aug 13, 2015 by avibootz
edited Apr 13, 2024 by avibootz
0 votes
#include <stdio.h>
 
int main() {
    char file[100] = "d:\\data.txt";
 
    int del = remove(file); // int remove(const char *filename) deletes the given filename
     
    if (!del) {
        printf("File Deleted successfully\n");
    }
    else {
        printf("Delete error\n");
    }
     
    return 0;
}
  
  
       
/*
run:
    
File Deleted successfully    
 
*/

 



answered Apr 13, 2024 by avibootz

Related questions

2 answers 255 views
2 answers 289 views
1 answer 179 views
179 views asked Jun 8, 2018 by avibootz
1 answer 94 views
94 views asked Jul 27, 2023 by avibootz
1 answer 126 views
126 views asked Jun 19, 2020 by avibootz
...