为什么C的"&的fopen QUOT;采取"为const char *"作为第二个参数? [英] Why does C's "fopen" take a "const char *" as its second argument?

查看:141
本文介绍了为什么C的"&的fopen QUOT;采取"为const char *"作为第二个参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它总是让我觉得奇怪的是,C函数的fopen需​​要为const char *作为第二个参数。我想这会更容易地阅读你的code和实现库的code,如果有在stdio.h中定义位掩码,如IO_READ和这样的,所以你可以做这样的事情:

It has always struck me as strange that the C function "fopen" takes a "const char *" as the second argument. I would think it would be easier to both read your code and implement the library's code if there were bit masks defined in stdio.h, like "IO_READ" and such, so you could do things like:

FILE* myFile = fopen("file.txt", IO_READ | IO_WRITE);

有一个纲领性的原因其实是,或只是历史的方式? (即这就是事情是这样的。)

Is there a programmatic reason for the way it actually is, or is it just historic? (i.e. "That's just the way it is.")

修改感谢大家解释。正确的答案可能是当中给出的。

EDIT Thanks for the explanations everyone. The correct answer is probably amongst those given.

推荐答案

一个字:遗产。不幸的是我们不得不忍受它。

One word : legacy. Unfortunately we have to live with it.

只是猜测:也许这在当时是为const char *似乎更灵活的解​​决方案,因为它不以任何方式限制。位掩码只能有32个不同的值。现在看起来像一个YAGNI给我。

Just speculation : Maybe at the time a "const char *" seemed more flexible solution, because it is not limited in any way. A bit mask could only have 32 different values. Looks like a YAGNI to me now.

更多的猜测:花花公子懒惰和写作RB需要较少的打字比MASK_THIS | MASK_THAT:)

More speculation : Dudes were lazy and writing "rb" requires less typing than MASK_THIS | MASK_THAT :)

这篇关于为什么C的"&的fopen QUOT;采取"为const char *"作为第二个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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