首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C unistd.h写()命令写额外字符

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

Stack Overflow用户
提问于 2018-02-15 11:19:02
回答 2查看 801关注 0票数 0

我一直试图在unix/linux中使用系统调用(read()、write()、open()、close())来实现cp命令。

但是,当我在终端上运行我的程序时,我会将这个程序的源代码复制到同一个目录中,并进行名称更改(源代码大约是300行)。

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

额外的线与200的线相同。它是从哪里来的?

这是截图编译时

argp.c

123.c

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

代码语言:javascript
复制
 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)

代码语言:javascript
复制
        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);

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-02-15 11:39:02

您总是尝试读取/写入BUFFERSIZE字节。但是,如果要复制的文件大小为BUFFERSIZE的倍数,会发生什么情况?你写上一次读过的东西。

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

而不是:

代码语言:javascript
复制
        pos = read(src, buffer, BUFFERSIZE);

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

使用:

代码语言:javascript
复制
        pos = read(src, buffer, BUFFERSIZE);

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

在这里相同,而不是:

代码语言:javascript
复制
        //write target file
        while ((pos = read(src, buffer, BUFFERSIZE)) > 0) {
           if (write(tgt, buffer, BUFFERSIZE) != pos) {
               exit(EXIT_FAILURE);
           }
        }

使用:

代码语言:javascript
复制
        //write target file
        while ((pos = read(src, buffer, BUFFERSIZE)) > 0) {
           if (write(tgt, buffer, pos) != pos) {
               exit(EXIT_FAILURE);
           }
        }
票数 0
EN

Stack Overflow用户

发布于 2018-02-15 11:38:17

无条件地写入一个完整的缓冲区,不管读取了多少:

代码语言:javascript
复制
 //write target file
        while ((pos = read(src, buffer, BUFFERSIZE)) > 0) {
           if (write(tgt, buffer, BUFFERSIZE) != pos) {
               exit(EXIT_FAILURE);
           }
        }

应:

代码语言:javascript
复制
 //write target file
        while ((pos = read(src, buffer, BUFFERSIZE)) > 0) {
           if (write(tgt, buffer, pos) != pos) {
               exit(EXIT_FAILURE);
           }
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48805975

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档