çFOPEN VS开放 [英] C fopen vs open

查看:107
本文介绍了çFOPEN VS开放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有你想使用的任何原因(除了那些句法等)

Is there any reason (other than syntactic ones) that you'd want to use

FILE *fdopen(int fd, const char *mode);

FILE *fopen(const char *path, const char *mode);

而不是

int open(const char *pathname, int flags, mode_t mode);

在Linux环境下使用C时,

when using C in a Linux environment?

推荐答案

有使用四个主要原因的fopen,而不是开放的。

There are four main reasons to use fopen instead of open.


  1. 的fopen 为您提供缓冲IO可能变成是一个很多比你与打开

  2. 的fopen 做行结束翻译,如果文件没有二进制模式,如果你的程序是不断移植到非Unix环境,这是非常有帮助的打开。

  3. A FILE * 让你使用的fscanf 等标准输入输出功能的能力。

  4. 您$​​ C $ C可能有一天会需要被移植到仅支持ANSI C和不支持打开函数一些其他的平台。

  1. fopen provides you with buffering IO that may turn out to be a lot faster than what you're doing with open.
  2. fopen does line ending translation if the file is not opened in binary mode, which can be very helpful if your program is ever ported to a non-Unix environment.
  3. A FILE * gives you the ability to use fscanf and other stdio functions.
  4. Your code may someday need to be ported to some other platform that only supports ANSI C and does not support the open function.

在我看来更多的时候结束翻译线会挡住你的方式比可以帮助你,和的fscanf的分析是如此之弱,你不可避免地最终有利于更有用的东西扔了出来。

In my opinion the line ending translation more often gets in your way than helps you, and the parsing of fscanf is so weak that you inevitably end up tossing it out in favor of something more useful.

这支持C大多数平台上有一个打开功能。

And most platforms that support C have an open function.

这使得缓冲问题。在地方,你主要是读取或写入文件的顺序一,缓冲的支持是真正有用的和一个大的速度提升。但是,它可能会导致在其中,当你想到它是有数据不能在文件中结束了一些有趣的问题。你要记住 FCLOSE fflush 在适当的时候。

That leaves the buffering question. In places where you are mainly reading or writing a file sequentially, the buffering support is really helpful and a big speed improvement. But it can lead to some interesting problems in which data does not end up in the file when you expect it to be there. You have to remember to fclose or fflush at the appropriate times.

如果你正在做的目的,缓冲的用处迅速下降。

If you're doing seeks, the usefulness of buffering quickly goes down.

当然,我的偏见是,我倾向于用插座的工作一大堆,而且事实上,你真正想要做非阻塞IO(其中 FILE * 完全失败,在任何合理的方式与根本没有缓冲的支持),经常有复杂的分析要求真我色彩的看法。

Of course, my bias is that I tend to work with sockets a whole lot, and there the fact that you really want to be doing non-blocking IO (which FILE * totally fails to support in any reasonable way) with no buffering at all and often have complex parsing requirements really color my perceptions.

这篇关于çFOPEN VS开放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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