使用c语言系统调用复制文件

时间:2013-09-10 19:17:07

标签: c system-calls

我正在用c编程语言探索系统调用,并尝试复制文件的一些例子。我遇到的问题是,我有一个原始文本文件包含一行“快速棕色狐狸...”要复制,然后结果包含不打算在那里的数据。关于如何解决这个问题我应该知道的任何想法或提示吗?

当我尝试运行代码示例./copy sample.txt new_sample.txt(类似于 cp 命令)时,我在终端上获得此输出:

$./copy sample.txt new_sample.txt
argv[0] = ./copy
argv[1] = sample.txt
argv[2] = new_sample.txt
ERROR: couldn't write whole buffer
Segmentation fault: 11
$

如果终端上有错误,则会创建一个名为new_sample.txt的文件,使用vim编辑器打开它会显示此结果。

  

快速的棕色狐狸跳过懒狗。   ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ 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;
    int openFlags;
    mode_t filePerms;
    ssize_t numRead;
    char buf[BUF_SIZE];

    /* check for the inputs */
    int arg_count=0;
    for (arg_count; arg_count < argc; arg_count++)
        printf("argv[%d] = %s\n", arg_count, argv[arg_count]);

    if (argc != 3 || strcmp(argv[1], "--help") == 0)
        usageErr("%s old-file new-file\n", argv[0]);

    /* open input and outpu files */
    inputFd = open(argv[1], O_RDONLY);
    if (inputFd == -1)
        errExit("opening file %s", argv);

    openFlags = O_CREAT | O_WRONLY | O_TRUNC;
    filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; /*rw-rw-rw*/

    outputFd = open(argv[2], openFlags, filePerms);

    if (outputFd == -1)
        errExit("opening file %s", argv[2]);

    /* transfer data until we encounter end of input or an error */

    while ((numRead = read(inputFd, buf, BUF_SIZE)) > 0)
    {
        if (write(outputFd, buf, BUF_SIZE) != numRead)
            fatal("couldn't write whole buffer");
    }

    if (numRead == -1)
        errExit("read");

    if (close(inputFd) == -1)
        errExit("close input");
    if (close(outputFd) == -1)
        errExit("close output");

    exit(EXIT_SUCCESS);
}

1 个答案:

答案 0 :(得分:2)

您的循环在文件末尾读取太多,因为该文件的长度可能不是BUF_SIZE的倍数。因此,如果文件中有20个字节,则会复制原始的20个字节,加上1004个字节的垃圾(缓冲区中的任何内容)。

执行此操作以将您读取的字节数写入输出:

while ((numRead = read(inputFd, buf, BUF_SIZE)) > 0)
{
    if (write(outputFd, buf, numRead) != numRead)
        fatal("couldn't write whole buffer");
}