For faster navigation, this Iframe is preloading the Wikiwand page for 写入.

写入

写入(write)是由类Unix操作系统内核提供的最为基本的程序。此程序从用户定义的缓冲中将数据写入设备或文件,这也是从程序中使用系统调用直接输出数据的主要方式。待写入的地点由文件描述符定义;要写入的数据(如文本)由指针和大小(字节数)定义。

write也因此需要三个参数:

  1. 文件代码(文件描述符fd)
  2. 指向文件存储地的缓冲的指针(buf);
  3. 要从缓冲中写入的字节数(nbytes)。

POSIX用法

[编辑]

写入调用接口[1][2]根据POSIX规范定义。通过调用写入函数,数据才能写入文件。此函数的原型为:

 ssize_t write(int fd, const void *buf, size_t nbytes);
参数 描述
fd
根据调用打开函数获取的文件描述符。此参数是一个可为0、1或2整数值,其值分别代表了标准输入、标准输出和标准错误。
buf
指向字符阵列,其内容为fd参数指向的文件。
nbytes
指定从字符阵列写入fd参数指向的文件的字节数。

在上述语法中,ssize_t是一个typedef(定义于stddef.h中带符号的数据类型)。但注意write()会在发生错误的时候返回-1(参见下方的错误一节),故其一定会返回带符号的值。 写入函数将返回成功写入数组的字节数,即意味着有时此值将小于指定的nbytes

使用示例

[编辑]
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

int main_gg (int argc, char *argv[])
{
    int fd1;
    char buf[128];
    fd1 = open(argv[1], O_WRONLY);
    if (fd1 == -1) {
        perror(argv[1]);
        return EXIT_FAILURE;
    }

    /* 输入待写入至文件的数据 */
    scanf("%127s", buf);

    write(fd1, buf, strlen(buf)); /* fd1为文件描述符,buf为用于存储数据的字符阵列,strlen(buf)用于通知函数缓冲中需要复制的字符串字节长度 */

    close(fd1);

    return 0;
}

操作错误

[编辑]

下方列出的是在写入文件时可能会发生的错误[3][4]。这些错误为errno.h中列出的宏命令。

错误数字 错误 意义
4
EINTR
系统调用中断。
5
EIO
低级错误,通常与硬件的读取/写入操作有关。
9
EBADF
文件描述符fd无效,或是正尝试写入打开为“只读”模式的文件。
13
EACCES
用户没有写入文件的所需权限。
14
EFAULT
函数中指定的地址无效。
22
EINVAL
函数传递的参数无效。
27
EFBIG
指定于nbytes中的文件大小过大,且大于系统所允许的值。
28
ENOSPC
待写入至的存储设备上的存储空间不足。
32
EPIPE
管道损坏,或是在管道另一端的文件不可用于I/O(大多数抛出此错误的进程同时会产生SIGPIPE信号)。

调用写入的高级I/O函数

[编辑]

虽然看起来很像,但写入系统调用不是一个普通的函数。例如在x86架构上的Linux系统上,此系统调用会使用INT 80H指令以将控制权转移给内核。[5]写入系统调用,及其配对函数读取(read)均是只能理解字节的低级函数。写入函数不可用于写入记录(如)。因此,此时通常需要高级的输入输出函数(如printf)。通常与混乱的低级接口相比,程序员更偏好使用高级接口。这些函数在内部调用其他函数,反之也可做出写入调用,进而打造出了分层函数组合。[6]

有了这种组合,高级函数可收集数据的字节并随后写入文件。

调用写入的高级I/O函数

參見

[编辑]
  • fwrite
  • getchar
  • fprintf
  • pwrite ()
  • read (system call)
  • sync (Unix)

参考文献

[编辑]

外部链接

[编辑]
{{bottomLinkPreText}} {{bottomLinkText}}
写入
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?