使用 open()、read() 和 write() 系统调用复制文件 [英] Using open(), read() and write() system calls to copy a file

查看:74
本文介绍了使用 open()、read() 和 write() 系统调用复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 read()write()open() 将文件复制到新目录时遇到问题.我可以使用 open() 创建一个新文件,并且可以使用 write() 写入终端,但我假设如果我将我的 int 文件描述符从 open 传递给 write,write 将写入指定的文件.

I'm having trouble using read(), write(), and open() to copy a file into a new directory. I'm able to create a new file with open() and I'm able to write to the terminal with write() but I was under the assumption that if I passed my int file descriptor from open to write, write would write to the specified file.

除此之外,一旦我打开一个文件,权限就变得很奇怪,我只能以 root 身份打开它.每次我创建一个文件时,它都是空的,但我可以在终端中看到我刚刚读取的整个文件.

In addition to this, once I open a file the permissions are all funky and I can only open it as root. Each time I create a file, it's completely empty but I can see the entire file I just read be printed out in the terminal.

我已经阅读了每个系统调用的 man 文件,但我仍然不明白这里的问题是什么.

I've read through the man files for each system call but I'm still not grasping what the issue is here.

这是我的代码:

void *scanFile( s_request *request )
{
    //TODO Print out all struct variables.
    pthread_mutex_lock(&mutex);

    int readFileReference;
    int writeFileReference;
    int bufferSize = request->bufferSize;
    int numberOfBytesRead = bufferSize;
    int *buffer[bufferSize];

    if ((readFileReference = open(request->file, O_RDONLY)) == -1)
    {
        printf("Failed to open file. Aborting.\n");
        exit(EXIT_FAILURE);
    }

    if ((writeFileReference = open("newfile.txt", O_CREAT | O_APPEND | O_RDWR) == -1))
    {
        printf("Failed to open write file. Aborting.\n");
        exit(EXIT_FAILURE);
    }

    while ((read(readFileReference, buffer, numberOfBytesRead)) != 0)
    {
        write(writeFileReference, buffer, bufferSize);
        bzero(buffer, bufferSize);
    }

    pthread_mutex_unlock(&mutex);
}

修复了问题:

更新代码:

void *scanFile( s_request *request )
{
    //TODO Print out all struct variables.
    pthread_mutex_lock(&mutex);

    int readFileReference;
    int writeFileReference;
    int bufferSize = request->bufferSize;
    int numberOfBytesRead = bufferSize;
    int *buffer[bufferSize];

    if ((readFileReference = open(request->file, O_RDONLY)) == -1)
    {
        printf("Failed to open file. Aborting.\n");
        exit(EXIT_FAILURE);
    }

    if ((writeFileReference = open("newfile.txt", O_CREAT | O_WRONLY, 0777)) == -1)
    {
        printf("Failed to open write file. Aborting.\n");
        exit(EXIT_FAILURE);
    }

    while ((read(readFileReference, buffer, numberOfBytesRead)) != 0)
    {
        write(writeFileReference, buffer, bufferSize);
        bzero(buffer, bufferSize);
    }

    close(writeFileReference);
    close(readFileReference);

    pthread_mutex_unlock(&mutex);
}

推荐答案

if ((writeFileReference = open("newfile.txt", O_CREAT | O_APPEND | O_RDWR) == -1))

您在这一行中的括号有误.(在 readFileReference 行是正确的.)你想要的是:

You've got the parentheses wrong on this line. (It's correct on the readFileReference line.) What you want is:

if ((writeFileReference = open("newfile.txt", O_CREAT | O_APPEND | O_RDWR)) == -1)
                                                                         ^^^    ^^^

该行的原始版本正在调用 open(),与 -1 进行比较,然后分配比较结果writeFileReference.

Your original version of that line was calling open(), performing a comparison to -1, then assigning the result of that comparison to writeFileReference.

另外:

  • 正如 Zan Lynx 在评论中指出的,您需要在创建文件时将权限值传递给 open().0666 通常是正确的——它会将文件创建为可读/可写.(它由进程 umask 修改,因此它最终会在典型配置下创建文件为 0644.)

  • As noted by Zan Lynx in a comment, you need to pass a permissions value to open() when creating a file. 0666 is typically correct -- it'll create the file as readable/writable. (It's modified by the process umask, so it'll end up creating the file as 0644 under a typical configuration.)

您需要将 read 的返回值保存在某处,并将其作为第三个参数传递给 write(而不是 bufferSize).否则,您的程序将写入比读取更多的字节,例如复制小文件时.

You need to save the return value from read somewhere and pass that as the third argument to write (instead of bufferSize). Otherwise, your program will write more bytes than were read, e.g. when copying small files.

从技术上讲,您应该检查 write() 的返回值.就像 read() 一样,不能保证每次调用都完成一次完整的写入.(实际上,在处理常规文件时,它总是会完成写入或返回错误,但在某些深奥的情况下可能并非如此.)

Technically, you should be checking the return value from write(). Just like read(), it's not guaranteed to complete a full write every time it's called. (In practice, when working with regular files, it will always either complete the write or return an error, but there are some esoteric situations where this may not be the case.)

从缓冲区写入后,您无需bzero().

You don't need to bzero() the buffer after writing from it.

这篇关于使用 open()、read() 和 write() 系统调用复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆