I'm having a hard time making a system call to unlink a file work in my C code. I want to remove the file from the filesystem after the code is copied. I am getting an error that says:
declared here extern int unlink (const char *__name) __THROW __nonnull ((1));
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
int main(int argc, char * args [])
{
int infile, outfile;
int numofbytesread;
char buffer[20];
infile = open(args[1], O_RDONLY, 0700);
if (infile == ENOENT)
{
printf("Could not find file");
return 1;
}
outfile == open(args[2], O_WRONLY | O_CREAT, 0700);
while ((numofbytesread = read(infile, buffer, 20))){
write(outfile, buffer, numofbytesread);
}
close(infile);
close(outfile);
unlink();
return 0;
}
After copying, you can invoke the unlink
system call.
unlink(args[1])
But be sure to check if the copy was successful before removing the file.
Ref: https://www.gnu.org/software/libc/manual/html_node/Deleting-Files.html
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments