gpt4 book ai didi

c - 我们可以在 `fdopen()` 中指定哪些类型,以及该类型的哪一部分是有效的?

转载 作者:太空宇宙 更新时间:2023-11-04 10:06:40 24 4
gpt4 key购买 nike

APUE 说

With fdopen, the meanings of the type argument differ slightly.

The descriptor has already been opened, so opening for writing does not truncate the file. (If the descriptor was created by the open function, for example, and the file already existed, the O_TRUNC flag would control whether the file was truncated. The fdopen function cannot simply truncate any file it opens for writing.)

Also, the standard I/O append mode cannot create the file (since the file has to exist if a descriptor refers to it).

一般来说,当我们在 open() 返回的文件描述符上调用 fdopen() 时,我们可以在 fdopen() 中指定哪些类型?fdopen() 中指定的类型必须与 open() 中指定的模式完全相同吗?fdopen() 中指定的类型是否可以是 open() 中指定模式的子集、超集,或者既不是子集也不是超集?

如果fdopen()中指定的类型相对于前面open()中指定的模式没有限制,则fdopen() 有效,哪一部分无效(即被忽略)?

谢谢。

最佳答案

POSIX指定:

The application shall ensure that the mode of the stream as expressed by the mode argument is allowed by the file access mode of the open file description to which fildes refers.

基本原理部分更进一步:

The meanings of the mode arguments of fdopen() and fopen() differ. With fdopen(), open for write (w or w+) does not truncate, and append (a or a+) cannot create for writing. The mode argument formats that include a b are allowed for consistency with the ISO C standard function fopen(). The b has no effect on the resulting stream. Although not explicitly required by this volume of IEEE Std 1003.1-2001, a good implementation of append (a) mode would cause the O_APPEND flag to be set.

由于文件描述符可以通过多种方式打开(open()socket() 等),并且可以有自定义设备驱动程序有不同的限制,实际上不可能提供 open() 模式和 fdopen() 模式之间关系的一般规范。

但是可以很安全地假设,如果文件描述符以写入方式打开,您应该能够使用模式 w,如果它以读取方式打开,您可以使用模式 r.

关于c - 我们可以在 `fdopen()` 中指定哪些类型,以及该类型的哪一部分是有效的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52103489/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com