user1031204
user1031204

Reputation: 701

Rename a file to trash

I am using rename function (C, under ubuntu) to move file from one folder to another when trying: rename("./t2.c", "./this/then_this/it_works.c"); it works wonders, removes the file from current folder and moves it to the then_this folder under name.

but when i try this: rename("./t2.c", "~/.local/share/Trash/files/it_works.c"); it just doesn't work, but in terminal typing in "cd ~/.local/share/Trash/files/it_works.c" does open the trash bin.

So what i'm trying to do is move a file to trash bin(delete it). Could anyone tell me what i am doing wrong?

Upvotes: 7

Views: 322

Answers (3)

Jan
Jan

Reputation: 11726

~ is an handy shortcut available only in shell. You can achieve a similar behaviour by using getenv("HOME") and concatenate it with the target directory.

snprintf(buffer, size_of_buffer, "%s/.local/share/Trash/files/it_works.c",
         getenv("HOME"));

Remember to #include <stdlib.h> and have a buffer which is at least PATH_MAX bytes long (limits.h) to store the result.

Upvotes: 4

Duck
Duck

Reputation: 27562

realpath() will be helpful here. You may want to look at glob() and wordexp() as well.

Upvotes: 4

NPE
NPE

Reputation: 500713

The problem is that ~ only works in the shell. Replace with the full absolute path (e.g. /home/user1031204/.local/...) & re-try.

Upvotes: 9

Related Questions