5

I am trying to delete a file in c program. Assume that the file is located in current directory of source file. I have searched a lot but didn't get any solution. Everyone is suggesting to use remove() function.

Here is my source code:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *fp;
    int delete_status;
    char del[50];
    printf("Enter a file name to delete it: ");
    gets(del);
    delete_status = remove(del);
    if(delete_status!=0) {
        printf("File can not be deleted!\nFile does not exist in current directory\n");
    }
    else printf("File %s has been deleted successfully!\n", del);
    return 0;
}

Is there any way to remove file without using remove() function. I want to code manually without using any other stl built in function.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 2
    Note that 50 is ridiculously short for a file name, and then using `gets()` compounds the problems. If the size was 4096, you'd be better off, but still vulnerable. [You cannot use `gets()` safely](http://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used), full stop. Also note that STL is normally associated with C++ and not with C. And it isn't clear what you mean by 'any other STL built in function'. System calls are not built in within the normal meaning of the term. – Jonathan Leffler Apr 19 '15 at 05:30
  • @Shahiduzzaman You want to do this is which Operating system ? Answers below are for linux i guess... – sps Apr 19 '15 at 05:56
  • 1
    `remove()` is Standard C. What is wrong with it? – alk Apr 19 '15 at 07:49
  • I want to do this in windows Operating System. All of you didn't understand my needs and requirement. I know that it is posible to delete a file using standard library function like remove(), unlink(), rm() etc. But I want to code manually without using any built in function. – Shahidujzaman Shahid Apr 20 '15 at 01:31
  • For example, We can find easily length of a string using built in function strlen(stringname). But it can be done also using a loop setting a counter variable. Actually I want to do this manually without using stl function. It is just a example for your clear understanding. Like this I want to delete a file using C or C++ program without using any other built in function like remove(), unlink(), rm(). – Shahidujzaman Shahid Apr 20 '15 at 01:36
  • @ShahiduzzamanShahid "I want to code manually." See my answer. – MD XF Mar 05 '17 at 00:42

6 Answers6

10

You can replace remove() with unlink() (for files) and rmdir() (for directories).

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
mfro
  • 3,286
  • 1
  • 19
  • 28
  • This assumes a POSIX-ish environment but is correct. Note that Standard C has no concept of directories and `remove()` is not required to remove them. POSIX does require `remove()` to handle files and directories. – Jonathan Leffler Apr 19 '15 at 05:23
  • Is there any way such as using loop to delete a file without using those built in function like remove(), unlike(). It is posible to read the file name? If posible then may be it can be done replacing every char of the file name with null char. – Shahidujzaman Shahid Apr 20 '15 at 01:40
4

You can check this answer. You should try to read a system programming book where you can learn about uses like INTERNAL_SYSCALL.

You can skim through the functions referred in the posts like unlink() etc.

EDIT: actually somehow you will end up using a system-call at some level. You probably trying to achieve the operation of deleting a file from different abstraction level.(remove() system call will also use INTERNAL_SYSCALL which nothing but a system call).

Now from low level deleting a file doesn't mean we are erasing something. We are just considering the space as free space(free pool of memory) and then any metadata related to that file is also freed. To achieve that you need to implement a filesystem that allocates memory,deletes it..using device level instructions.

Community
  • 1
  • 1
user2736738
  • 30,591
  • 5
  • 42
  • 56
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Hans Z. Apr 19 '15 at 06:26
  • @HansZ..: Check my post..I have added few words understood by me. If any further modification needed I will look forward to it. – user2736738 Apr 19 '15 at 06:48
1

Call unlink for files, rmdir for directories. You could easily check which a file is using stat and then call the correct function.

struct stat sb;

if (!stat(filename, &sb))
{
    if (S_ISDIR(sb.st_mode))
        rmdir(filename);
    else
        unlink(filename);
}

Include <sys/stat.h> for stat and S_ISDIR, <unistd.h> for rmdir and unlink.

Oh, and per your comment:

All of you didn't understand my needs and requirement. I know that it is posible to delete a file using standard library function like remove(), unlink(), rm() etc. But I want to code manually without using any built in function.

Have fun reproducing unlink's source code.

Community
  • 1
  • 1
MD XF
  • 7,860
  • 7
  • 40
  • 71
0

I think what you need to know is unlink() function. For deleting files, remove() internally calls unlink() itself. Check the man page for details.

However, first I suggest you to change the gets() with fgets(). Also, int main() should be int main(void).

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

One can use fork and exec to run the rm command over shell.

sample code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>


int main(){
        int status;
        pid_t pid = fork();
        if(-1 == pid){
                printf("fork() failed");
                exit(EXIT_FAILURE);
        }else if(pid == 0){
                execl("/bin/sh", "sh", "-c", "rm /tmp/sandeep_reve.txt", (char *) NULL);
        }else{
                printf("Fork with id %ld\n",(long)pid);
                waitpid(pid,&status,0);
        }
return 0;
}
Sandeep_black
  • 1,352
  • 17
  • 18
-2

Use system():

PART I:(Delete file)

#include <stdio.h>
#include <string.h> 
int main()
{
    FILE *fp;
    int delete_status;
    char path[100],order[]="del ";//del for delete file, if change del to rd is delete folder(**Code at part 2)
    printf("Enter a path of file to delete it: ");
    gets(path);
    strcat(order,path);//Order
    fp = fopen(path,"r");
    if(fp != NULL){//Check file whether or not exist
       fclose(fp);
       system(order);//Del file
       printf("Delete successfully");
    }else{
       perror("ERROR");
       fclose(fp);
    }

    return 0;
}

For example, you want to delete 1.txt.Then, you may put the c program in the same file and then intput 1.txt or enter whole path of the file.(e.g C:\User\desktop\1.txt)

PART II :(Delete folder)

#include <stdio.h>
#include <string.h> 
int main()
{
    FILE *fp;
    int delete_status,i = 1;
    char path[100],order[] = "rd ";//del -> rd

    printf("Enter a path of file to delete it: ");
    gets(path);
    strcat(order,path);

    system(order);

    return 0;
}
whalesf
  • 49
  • 1
  • 8
  • My answer is basic on windows OS. So, you may need to modify the program in other OS. But, i think the principle is the same. – whalesf Apr 19 '15 at 06:12
  • Is it not posible to a file reading the file name and replaceing every char of filename with null char. If I use system() then what is the differece between remove() and system()? Please avoid any other built in function and use your own simple algorithm to delete a file. – Shahidujzaman Shahid Apr 20 '15 at 01:56
  • using the system() call can open security issues to your program. use unlink call to remove the file – Eswar Yaganti Mar 14 '16 at 02:42