在 Linux 中复制/移动文件并使用系统调用的 C 应用程序

标签 c linux

<分区>

我很难在我的 C 代码中进行系统调用以取消链接文件工作。我想在复制代码后从文件系统中删除文件。我收到一条错误消息:

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

关于在 Linux 中复制/移动文件并使用系统调用的 C 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41927880/

相关文章:

linux - 除了 strace 之外,还有其他方法可以记录 ioctl 调用吗?

database - 与谷歌应用程序引擎兼容的简单网络应用程序

c - 进程使用 fork 运行 xterm 实例时出错

linux - super 终端使用的TCP/IP通信协议(protocol)?

无法弄清楚为什么我在初始化数组时遇到段错误

php - 发送 : How to correctly destruct a custom object in PHP 7?

使用 getcwd 在 C 字符串中始终获取空值

c - gsl 复数矩阵 * 复数 vector

c - 使用完帧缓冲区后如何恢复控制台测试?

c# - 捕获 .NET Core 中正常关闭的关闭命令