简体   繁体   English

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

[英]C unistd.h write() command write extra character

I've been trying to do c programming implementation of cp command in unix/linux by using system calls (read(), write(), open(), close()). 我一直在尝试通过使用系统调用(read(),write(),open(),close())在unix / linux中对cp命令进行c编程实现。

But when I run my program through terminal by copying my source code of this program to the same directory with name change (the source code is about 300 lines) 但是当我通过终端运行程序时,将我的程序源代码复制到相同的目录(更改名称)(源代码大约300行)

and when I open that output file , it has more character than the original file. 当我打开该输出文件时,它的字符比原始文件更多。

The extra line is the same as 200ish line. 多余的线与200ish线相同。 Where does it came from? 它来自哪里?

here's the screenshot when compile 这是编译时的屏幕截图

argp.c argp.c

123.c 123.c

This is the source code near the end of the original file(argp.c). 这是原始文件(argp.c)末尾附近的源代码。 You will see how I use the read write method. 您将看到我如何使用读写方法。

 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);

}

This is the source code near the end of the copy file(123.c) 这是复制文件末尾附近的源代码(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);

}

You unconditionally write a full buffer, regardless of how much was read: 您无条件地写了一个完整的缓冲区,无论读取了多少:

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

should be: 应该:

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

You always try to read/write BUFFERSIZE bytes. 您始终尝试读取/写入BUFFERSIZE字节。 But what happens if the file you want to copy has a size multiple of BUFFERSIZE ? 但是,如果要复制的文件的大小是BUFFERSIZE倍数,会发生什么? You write what have been read last time. 您写上次阅读的内容。

read return the number of bytes read, so each write should try to write this number of bytes: read返回read的字节数,因此每次write应尝试写入此字节数:

Instead of: 代替:

        pos = read(src, buffer, BUFFERSIZE);

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

Use: 采用:

        pos = read(src, buffer, BUFFERSIZE);

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

Same here, instead of: 此处相同,而不是:

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

Use: 采用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM