C unistd.h write() 命令写入额外字符

标签 c linux cp

我一直在尝试通过使用系统调用(read()、write()、open()、close())在 unix/linux 中执行 cp 命令的 c 编程实现。

但是当我通过终端运行我的程序时,通过将该程序的源代码复制到同一个目录并更改名称(源代码大约 300 行)

当我打开那个输出文件时,它比原始文件有更多的字符。

额外的行与 200ish 行相同。它从哪里来?

这是截图 when compile

argp.c

123.c

这是原始文件 (argp.c) 末尾附近的源代码。您将看到我如何使用读写方法。

 while (count - ind > 1) {

        strcpy(cdir, argv[argc-1]);

        if (!isFile(cdir)) {
            strcat(cdir, basename(arguments.argv[ind]));
        }

        if (arguments.update) {
            stat(cdir,&stDest);
            stat(arguments.argv[ind],&stSrc);
            if (difftime(stDest.st_mtim.tv_sec, stSrc.st_mtim.tv_sec) > 0 ) {
                printf("Destination file is newer\n");
                exit(EXIT_FAILURE);
            }
        }

        //open source file
        src = open(arguments.argv[ind],O_RDONLY);
        //if source file can't be opened
        if (src == -1) {
            printf("\nError opening file %s errno = %d\n",arguments.argv[ind],errno);
            exit(EXIT_FAILURE);
        }

        //open target file
        if (arguments.force) {
            //with -f option(default)
            tgt = open(cdir, O_WRONLY | O_CREAT | O_TRUNC , S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH);
        } else {
            //with -n option
            tgt = open(cdir, O_WRONLY | O_CREAT | O_EXCL , S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH);
        }
        //if the target file cannot be read or already exist(with -n option)
        if (tgt == -1) {
            printf("File exist or it's not a file.\nCan't copy.\n");
            exit(EXIT_FAILURE);
        }

        //read source file


        //write target file
        while ((pos = read(src, buffer, BUFFERSIZE)) > 0) {
           if (write(tgt, buffer, BUFFERSIZE) != pos) {
               exit(EXIT_FAILURE);
           }
        }

        //if the source file cannot be read
        if (pos == -1) {
            printf("\nError in reading data from %s\n",arguments.argv[ind]);
        }

        //close source file
        if (close(src) == -1) {
            printf("\nError in closing file %s\n",arguments.argv[ind]);
        }

        //close target file
        if (close(tgt) == -1) {
            printf("\nError in closing file %s\n",cdir);
        }

        ind++;
    }

    if (arguments.verbose) {
        printf("Copy successfully!\n");
    }

    exit(EXIT_SUCCESS);

}

这是复制文件(123.c)末尾附近的源代码

        while (count - ind > 1) {

        strcpy(cdir, argv[argc-1]);

        if (!isFile(cdir)) {
            strcat(cdir, basename(arguments.argv[ind]));
        }

        if (arguments.update) {
            stat(cdir,&stDest);
            stat(arguments.argv[ind],&stSrc);
            if (difftime(stDest.st_mtim.tv_sec, stSrc.st_mtim.tv_sec) > 0 ) {
                printf("Destination file is newer\n");
                exit(EXIT_FAILURE);
            }
        }

        //open source file
        src = open(arguments.argv[ind],O_RDONLY);
        //if source file can't be opened
        if (src == -1) {
            printf("\nError opening file %s errno = %d\n",arguments.argv[ind],errno);
            exit(EXIT_FAILURE);
        }

        //open target file
        if (arguments.force) {
            //with -f option(default)
            tgt = open(cdir, O_WRONLY | O_CREAT | O_TRUNC , S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH);
        } else {
            //with -n option
            tgt = open(cdir, O_WRONLY | O_CREAT | O_EXCL , S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH);
        }
        //if the target file cannot be read or already exist(with -n option)
        if (tgt == -1) {
            printf("File exist or it's not a file.\nCan't copy.\n");
            exit(EXIT_FAILURE);
        }

        //read source file


        //write target file
        while ((pos = read(src, buffer, BUFFERSIZE)) > 0) {
           if (write(tgt, buffer, BUFFERSIZE) != pos) {
               exit(EXIT_FAILURE);
           }
        }

        //if the source file cannot be read
        if (pos == -1) {
            printf("\nError in reading data from %s\n",arguments.argv[ind]);
        }

        //close source file
        if (close(src) == -1) {
            printf("\nError in closing file %s\n",arguments.argv[ind]);
        }

        //close target file
        if (close(tgt) == -1) {
            printf("\nError in closing file %s\n",cdir);
        }

        ind++;
    }

if (arguments.verbose) {
    printf("Copy successfully!\n");
}

exit(EXIT_SUCCESS);

}
it(EXIT_FAILURE);
        }

        //read source file


        //write target file
        while ((pos = read(src, buffer, BUFFERSIZE)) > 0) {
           if (write(tgt, buffer, BUFFERSIZE) != pos) {
               exit(EXIT_FAILURE);
           }
        }

        //if the source file cannot be read
        if (pos == -1) {
            printf("\nError in reading data from %s\n",arguments.argv[ind]);
        }

        //close source file
        if (close(src) == -1) {
            printf("\nError in closing file %s\n",arguments.argv[ind]);
        }

        //close target file
        if (close(tgt) == -1) {
            printf("\nError in closing file %s\n",cdir);
        }

        ind++;
    }

if (arguments.verbose) {
    printf("Copy successfully!\n");
}

exit(EXIT_SUCCESS);

}

最佳答案

您总是尝试读/写 BUFFERSIZE 字节。但是,如果您要复制的文件的大小是 BUFFERSIZE 的倍数,会发生什么情况?您写下上次阅读的内容。

read 返回读取的字节数,所以每次 write 都应该尝试写入这个字节数:

代替:

        pos = read(src, buffer, BUFFERSIZE);

        //write target file
        while (pos > 0) {
            write(tgt, buffer, BUFFERSIZE);
            pos = read(src, buffer, BUFFERSIZE);
        }

使用:

        pos = read(src, buffer, BUFFERSIZE);

        //write target file
        while (pos > 0) {
            write(tgt, buffer, pos);
            pos = read(src, buffer, BUFFERSIZE);
        }

同样在这里,而不是:

        //write target file
        while ((pos = read(src, buffer, BUFFERSIZE)) > 0) {
           if (write(tgt, buffer, BUFFERSIZE) != pos) {
               exit(EXIT_FAILURE);
           }
        }

使用:

        //write target file
        while ((pos = read(src, buffer, BUFFERSIZE)) > 0) {
           if (write(tgt, buffer, pos) != pos) {
               exit(EXIT_FAILURE);
           }
        }

关于C unistd.h write() 命令写入额外字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48805975/

相关文章:

c++ - C 到 C++ 指针数组的转换问题

c - 函数调用+计算返回值+比较C中的计算值

hadoop -cp 限制文件数量

shell - cp 仅非隐藏文件

Python3 CGI HTTPS 服务器在 Unix 上失败

c++ - waitpid 似乎没有进食

c - 几百次运行后 fopen() 内存泄漏

acosf 实现中的 CUDA __float_as_int

linux - 当我尝试安装 yuvmotionfps 时,无法识别的命令行选项 '-march'

linux - 非零返回码,尽管 find -exec rm 有效