Linux C fopen vs open

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1658476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:51:24  来源:igfitidea点击:

C fopen vs open

clinuxunixfile-iofopen

提问by LJM

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

您是否有任何理由(语法除外)想要使用

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

or

或者

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

instead of

代替

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

when using C in a Linux environment?

在 Linux 环境中使用 C 时?

采纳答案by Omnifarious

First, there is no particularly good reason to use fdopenif fopenis an option and openis the other possible choice. You shouldn't have used opento open the file in the first place if you want a FILE *. So including fdopenin that list is incorrect and confusing because it isn't very much like the others. I will now proceed to ignore it because the important distinction here is between a C standard FILE *and an OS-specific file descriptor.

首先,没有特别好的理由使用fdopeniffopen是一个选项并且open是另一个可能的选择。你不应该用来open打开文件中的第一个地方,如果你想要一个FILE *。因此,包含fdopen在该列表中是不正确且令人困惑的,因为它与其他列表不太一样。我现在将继续忽略它,因为这里的重要区别在于 C 标准FILE *和特定于操作系统的文件描述符之间。

There are four main reasons to use fopeninstead of open.

使用fopen代替有四个主要原因open

  1. fopenprovides you with buffering IO that may turn out to be a lot faster than what you're doing with open.
  2. fopendoes 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 (though the world appears to be converging on LF-only (except IETF text-based networking protocols like SMTP and HTTP and such)).
  3. A FILE *gives you the ability to use fscanfand 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 openfunction.
  1. fopen为您提供缓冲 IO,结果可能比您正在做的要快得多open
  2. fopen如果文件不是以二进制模式打开,则执行行结束转换,如果您的程序曾经被移植到非 Unix 环境,这会非常有用(尽管世界似乎只收敛于 LF(除了 IETF 基于文本的网络) SMTP 和 HTTP 等协议))。
  3. AFILE *使您能够使用fscanf和其他 stdio 函数。
  4. 您的代码可能有一天需要移植到其他仅支持 ANSI C 且不支持该open功能的平台。

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

在我看来,行尾翻译往往会妨碍您而不是帮助您,并且 的解析fscanf非常薄弱,以至于您不可避免地最终将其抛弃以支持更有用的东西。

And most platforms that support C have an openfunction.

并且大多数支持 C 的平台都有一个open函数。

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 fcloseor fflushat the appropriate times.

这就留下了缓冲问题。在您主要按顺序读取或写入文件的地方,缓冲支持确实很有帮助,并且速度大大提高。但它可能会导致一些有趣的问题,即当您期望数据存在时,数据并未最终出现在文件中。你必须记住fclosefflush在适当的时候。

If you're doing seeks (aka fsetposor fseekthe second of which is slightly trickier to use in a standards compliant way), the usefulness of buffering quickly goes down.

如果您正在执行搜索(又名fsetposfseek以符合标准的方式使用其中的第二个稍微有点棘手),缓冲的实用性会迅速下降。

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.

当然,我的偏见是我倾向于大量使用套接字,并且事实上你真的想要做非阻塞 IO(FILE *完全无法以任何合理的方式支持),而且经常没有缓冲有复杂的解析要求确实影响了我的看法。

回答by dreamlax

If you have a FILE *, you can use functions like fscanf, fprintfand fgetsetc. If you have just the file descriptor, you have limited (but likely faster) input and output routines read, writeetc.

如果你有FILE *,你可以像使用功能fscanffprintf以及fgets等,如果你刚才的文件描述符,你有有限的(但可能更快)的输入和输出程序readwrite等等。

回答by Emil H

open()is a low-level os call. fdopen()converts an os-level file descriptor to the higher-level FILE-abstraction of the C language. fopen()calls open()in the background and gives you a FILE-pointer directly.

open()是一个低级操作系统调用。fdopen()将操作系统级别的文件描述符转换为 C 语言的更高级别的 FILE 抽象。在后台fopen()调用open()并直接为您提供文件指针。

There are several advantages to using FILE-objects rather raw file descriptors, which includes greater ease of usage but also other technical advantages such as built-in buffering. Especially the buffering generally results in a sizeable performance advantage.

使用 FILE 对象而不是原始文件描述符有几个优点,其中包括更易于使用以及其他技术优势,例如内置缓冲。尤其是缓冲通常会带来相当大的性能优势。

回答by user7116

Unless you're part of the 0.1% of applications where using openis an actual performance benefit, there really is no good reason not to use fopen. As far as fdopenis concerned, if you aren't playing with file descriptors, you don't need that call.

除非你是 0.1% 的应用程序的一部分,在这些应用程序中使用open是一个实际的性能优势,否则真的没有充分的理由不使用fopen. 至于fdopen来讲,如果你不与文件描述符玩,你不需要这一呼吁。

Stick with fopenand its family of methods (fwrite, fread, fprintf, et al) and you'll be very satisfied. Just as importantly, other programmers will be satisfied with your code.

坚持使用fopen及其系列方法(fwritefreadfprintf、 等),您会非常满意。同样重要的是,其他程序员会对您的代码感到满意。

回答by digy

Using open, read, write means you have to worry about signal interaptions.

使用 open、read、write 意味着您必须担心信号交互。

If the call was interrupted by a signal handler the functions will return -1 and set errno to EINTR.

如果调用被信号处理程序中断,函数将返回 -1 并将 errno 设置为 EINTR。

So the proper way to close a file would be

所以关闭文件的正确方法是

while (retval = close(fd), retval == -1 && ernno == EINTR) ;

回答by Ersatz Splatt

I changed to open() from fopen() for my application, because fopen was causing double reads every time I ran fopen fgetc . Double reads were disruptive of what I was trying to accomplish. open() just seems to do what you ask of it.

我的应用程序从 fopen() 更改为 open() ,因为每次运行 fopen fgetc 时, fopen 都会导致双重读取。双重阅读破坏了我试图完成的工作。open() 似乎只是按照你的要求去做。

回答by prashad

opening a file using fopen
before we can read(or write) information from (to) a file on a disk we must open the file. to open the file we have called the function fopen.

在我们可以从(到)磁盘上的文件读取(或写入)信息之前,使用fopen
打开文件我们必须打开该文件。要打开我们调用函数 fopen 的文件。

1.firstly it searches on the disk the file to be opened.
2.then it loads the file from the disk into a place in memory called buffer.
3.it sets up a character pointer that points to the first character of the buffer.

this the way of behaviour of fopenfunction
there are some causes while buffering process,it may timedout. so while comparing fopen(high level i/o) to open(low level i/o) system call , and it is a faster more appropriate than fopen.

这种fopen函数的行为方式
在缓冲进程时有一些原因,它可能会超时。因此,在比较fopen(高级别 i/o)和open(低级别 i/o)系统调用时,它比fopen更快更合适。

回答by Arun Chettoor

open()is a system call and specific to Unix-based systems and it returns a file descriptor. You can write to a file descriptor using write()which is another system call.
fopen()is an ANSI C function call which returns a file pointer and it is portable to other OSes. We can write to a file pointer using fprintf.

open()是一个系统调用,特定于基于 Unix 的系统,它返回一个文件描述符。您可以使用write()另一个系统调用写入文件描述符。
fopen()是一个 ANSI C 函数调用,它返回一个文件指针,它可以移植到其他操作系统。我们可以使用fprintf.

In Unix:
You can get a file pointer from the file descriptor using:

在 Unix 中:
您可以使用以下方法从文件描述符中获取文件指针:

fP = fdopen(fD, "a");

You can get a file descriptor from the file pointer using:

您可以使用以下方法从文件指针获取文件描述符:

fD = fileno (fP);

回答by theadnangondal

open()will be called at the end of each of the fopen()family functions. open()is a system call and fopen()are provided by libraries as a wrapper functions for user easy of use

open()将在每个fopen()系列函数的末尾被调用。open()是一个系统调用,fopen()由库提供作为包装函数以方便用户使用

回答by Yogeesh H T

fopen vs open in C

fopen 与 C 中的打开

1) fopenis a library functionwhile openis a system call.

1)fopen是一个库函数open而是一个系统调用

2) fopenprovides buffered IOwhich is faster compare to openwhich is non buffered.

2)fopen提供缓冲IO被更快比较open非缓冲的

3) fopenis portablewhile opennot portable(open is environment specific).

3)fopen移植open不可移植开放是特定于环境的)。

4) fopenreturns a pointer to a FILE structure(FILE *); openreturns an integer that identifies the file.

4)fopen返回一个指向FILE 结构的指针(FILE *)open返回一个标识文件的整数。

5) A FILE *gives you the ability to use fscanfand other stdio functions.

5) AFILE *使您能够使用fscanf和其他 stdio 函数。