Remove a file in c with arguments and unlink() function

6.4k views Asked by At

I've been trying to make a program where I can remove a file with an unlink() function. I should be able to insert an argument (name of the file) and receive it, then removing it. For some reason it tells me the file got removed but does't remove at all. Here's what i got so far. I shouldn't use fopen() and functions like that. The files are created in the path /home/me/TP1/Users where all the "Users" are created. What am I doing wrong? Also, I am developing this in UNIX.

int main (int argc, char **argv) 
{
    char user [10];
    char *path = argv[1];
    int result;
    const char *filename = "/home/guima/TP1/Users";
    result = access (filename, F_OK);
    int fd;
    fd = open(argv[1], O_RDONLY);
    if (result == 0)
    {
        unlink(argv[1]);
        write(1,"Deleted!\n\n", 35);            
    }    
    else
    {
        write(1,"Error!\n\n",30); 
    }

    close(fd);
    return 0;
}
0

There are 0 answers