0

我正在探索 c 编程语言中的系统调用并尝试一些有关复制文件的示例。我遇到了一个问题,我有一个原始文本文件,其中包含要复制的单行“The quick brown fox ...”,然后结果包含不打算存在的数据。我应该知道如何解决这个问题的任何想法或提示?

当我尝试运行代码示例 ./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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^?^@^@<9b >¿¾dÿ^?^@^@Àú½dÿ^?^@^@^_ã¾dÿ^?^@^@@ü½dÿ^?^@^@<90>û½dÿ^?^@^@@û½dÿ^?^@^@^Z^S¾dÿ^?^@^@^@^@^@^@^@^@^@^@^@^@þ^D ^A^@^@^@À^E¾dÿ^?^@^@^@^@^@^@ÿ^?^@^@ ^G¾dÿ^?^@^@0^G¾dÿ^?^@^@^ @^@^@^@^C^@^@^@pû½dÿ^?^@^@×^Eä¦÷[<87>´^@^@^@^@^@^@^@^@^@ ^@^@^@^@^@^@^@

这是我的示例代码:(文件名:copy.c)

#include <sys/stat.h>
#include <fcntl.h>
#include "tlpi_hdr.h" /* contains stdlib, sys/types, stdio.h, unistd, error, string.h */

#ifndef BUF_SIZE /* Allow "cc -D" to override definition */
#define BUF_SIZE 1024
#endif

int main(int argc, char *argv[])
{
    int inputFd;
    int outputFd;
    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);
}
4

1 回答 1

3

您的循环在文件末尾读取太多,因为文件可能不是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");
}
于 2013-09-10T19:22:42.400 回答