我很难进行系统调用来取消链接我的C代码中的文件工作.我希望在复制代码后从文件系统中删除该文件.我收到的错误是: declared here extern int unlink (const char *__name) __THROW __nonnull ((1)); #i
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; }复制后,您可以调用unlink系统调用.
unlink(args[1])
但在删除文件之前,请务必检查副本是否成功.
参考:https://www.gnu.org/software/libc/manual/html_node/Deleting-Files.html