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.

6
  • 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, 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. Commented Apr 19, 2015 at 5:30
  • @Shahiduzzaman You want to do this is which Operating system ? Answers below are for linux i guess... Commented Apr 19, 2015 at 5:56
  • 1
    remove() is Standard C. What is wrong with it? Commented Apr 19, 2015 at 7: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. Commented Apr 20, 2015 at 1: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(). Commented Apr 20, 2015 at 1:36

6 Answers 6

10

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.
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.

2 Comments

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.
@HansZ..: Check my post..I have added few words understood by me. If any further modification needed I will look forward to it.
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.

Comments

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).

Comments

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;
}

Comments

-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;
}

3 Comments

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.
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.
using the system() call can open security issues to your program. use unlink call to remove the file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.