gpt4 book ai didi

C fopen模式参数

转载 作者:太空狗 更新时间:2023-10-29 15:32:59 25 4
gpt4 key购买 nike

为什么 C 中 fopen 的 'mode' 参数由字符串给出?将它作为位掩码或类似的东西会更有意义(以我的思维方式)。字符串所需的开销是低效且不必要的。

最佳答案

C11 §7.21.5.3 The fopen function

The argument mode points to a string. If the string is one of the following, the file is open in the indicated mode. Otherwise, the behavior is undefined.271)

在脚注中:

271) If the string begins with one of the above sequences, the implementation might choose to ignore the remaining characters, or it might use them to select different kinds of a file (some of which might not conform to the properties in 7.21.2

根据 C99 基本原理,委员会认为实现可以选择使用 mode 而不是标志:

Rationale for International Standard — Programming Languages — C §7.19.5.3 The fopen function

An implementation may choose to allow additional file specifications as part of the mode string argument. For instance,

file1 = fopen(file1name, "wb,reclen=80");

might be a reasonable extension on a system that provides record-oriented binary files and allows a programmer to specify record length.

GNU libc 有一个扩展,允许 mode 包含 ccs=STRING,参见 glibc manual

关于C fopen模式参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18999459/

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