Summarize the difference between open and fopen

Source: Internet
Author: User

https://www.zybuluo.com/yiltoncent/note/87461

Reference Link 1
Reference Link 2

For many beginners, it's not easy to figure out what's different about the two-name functions that are very similar to each other, except that they are used in function usage. If you can distinguish between the two, I believe you have a better understanding of C language and UNIX systems (including Linux).

Find some information on the internet, but feel not comprehensive, some of the answers are only from a certain point of view, so let people think, this is also right, that too. But whose statement is more correct? In fact, all are right, but the angle of interpretation is different. The following combination of personal understanding to do some carding.

1. Source

From the source point of view, the two can be well separated, which is the most obvious difference between the two:

    • openis the UNIX system call function (including Linux, etc.), which returns the file descriptor (descriptor), which is the index of the file in the File descriptor table.
    • fopenis the C language library function in the Ansic standard, which should be called different kernel APIs in different systems. Returns a pointer to the file structure.
      PS: From the source point of view, the two are inextricably linked, after all, C language library functions need to call the system API implementation.
2. Portability

This is inferred from the above sources that ' fopen ' is a C standard function and therefore has good portability, while ' open ' is a UNIX system call with limited portability. Similar features under Windows Use the API function ' CreateFile '.

3. Scope of application
    • openThe file descriptor is returned, and the file descriptor is an important concept under the UNIX system, and all devices under Unix operate as files. such as network sockets, hardware devices and so on. Of course including the operation of ordinary regular files (Regular file).
    • The fopen is used to manipulate regular regular files (Regular file).
4. File IO Hierarchy

From the point of view of file Io, the former belongs to the low-level IO function, which belongs to the advanced IO function. The simple distinction between low and advanced is: who is closer to the system kernel. Low-level file IO runs in the kernel state, and advanced file IO runs in the user state.

5. Buffering
    1. Buffered file system
      The buffered file system is characterized by a "buffer" in memory that is used for every file in the program, and when a read file is performed, the data is read into the memory "buffer" from the disk file, and the required data is read from the memory "buffer" as it fills. When performing a write operation, the data is written to the memory "buffer" and the Memory "buffer" is filled before writing to the file. It can be seen that the size of memory "buffer", affecting the number of actual operation external memory, memory "buffer" the greater the number of operations external memory, the execution speed is fast, high efficiency. In general, the size of the file "buffer" varies depending on the machine. fopen, fclose, fread, fwrite, fgetc, fgets, fputc, fputs, freopen, fseek, ftell, rewindand so on.
    2. Non-buffered file system
      Buffer file system is the use of file structure pointer to manage the file, through the file pointer to access the file, can read and write characters, strings, formatted data, can also read and write binary data. Non-buffered file system relies on the operating system, through the function of the operating system to read and write files, is a system-level input and output, it does not set the file structure pointer, only read and write binary files, but high efficiency, fast, because the ANSI standard no longer includes non-buffered file system, it is recommended that you do not choose it. open, close, read, write, getc, getchar, putc, putcharand so on.

To sum up, open there is no buffer, fopen there is buffering. The former is used in conjunction with, etc., the read write latter with fread , and fwrite so on.

Using the fopen function, because of the buffer in the user state, so when the file read and write operations to reduce the user state and the kernel state switch (switch to the kernel state call or call the system call API read : write ), and use open the function, When the file reads and writes, it is necessary to switch the kernel state and the user state every time, as if the sequence accesses the file, the fopen series function is open faster than the function calling the series directly, if the file is accessed randomly.

Such a summary, I believe that we have a more comprehensive and clear understanding of the two functions and series functions, you should also know where to use what kind of function is more appropriate, more efficient.

Summarize the difference between open and fopen

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.