What are the differences between open and fopen in Linux?

青灯夜游
Release: 2022-04-29 19:00:28
Original
5072 people have browsed it

Difference: 1. open is a UNIX system call function, while fopen is a C language library function in the ANSIC standard; 2. open is not as portable as fopen; 3. fopen can only manipulate ordinary regular files, while open can operate ordinary files, network sockets, etc.; 4. open has no buffering, while fopen has buffering.

What are the differences between open and fopen in Linux?

#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

linux system: the difference between open and fopen

1. Source

From the source From a perspective, the two can be distinguished very well. This is also the most obvious difference between the two:

open is a UNIX system call function (including LINUX, etc.) and returns a file description. symbol (f'd), which is the index of the file in the file descriptor table;

fopen is a C language library function in the ANSIC standard, which should be called differently in different systems. The kernel API. What is returned is a pointer to the file structure.

2. Portability

This can be inferred from the above sources. `fopen` is a C standard function, so it has good portability; and` open` is a UNIX system call with limited portability. For similar functions under windows, use the API function `CreateFile`.

3. Scope of application

#open Returns the file descriptor, and the file descriptor is an important concept under UNIX system. All devices operate in the form of files. Such as network sockets, hardware devices, etc. Of course, including operating regular files (Regular File).

fopen is used to manipulate ordinary regular files (Regular File).

4. File IO level

From the perspective of file IO, the former is a low-level IO function, and the latter is a high-level IO function. The simple distinction between low-level and high-level is: who is closer to the system kernel. Low-level file IO runs in the kernel mode, and high-level file IO runs in the user mode.

5. Buffering

  • Buffering file system
    The characteristics of the buffering file system are: opening up a "buffer" in the memory for Every file in the program is used; when performing a file reading operation, the data is first read from the disk file into the memory "buffer", and after it is full, the required data is read out from the memory "buffer" accordingly. When performing a file writing operation, the data is first written into the memory "buffer", and then the file is written after the memory "buffer" is full. It can be seen from this that the size of the memory "buffer" affects the actual number of external memory operations. The larger the memory "buffer", the fewer the number of external memory operations, and the execution speed is faster and more efficient. Generally speaking, the size of the file "buffer" depends on the machine. fopen, fclose, fread, fwrite, fgetc, fgets, fputc, fputs, freopen, fseek, ftell, rewind, etc.

  • Non-buffered file system
    The buffered file system manages files with the help of file structure pointers, and accesses files through file pointers. It can read and write characters, characters string, formatted data, and can also read and write binary data. The non-buffered file system relies on the operating system. Reading and writing files through the functions of the operating system is system-level input and output. It does not have a file structure pointer and can only read and write binary files, but it is highly efficient and fast. The ANSI standard no longer covers unbuffered file systems, so it is recommended not to choose it. open, close, read, write, getc, getchar, putc, putcharetc.

To sum it up in one sentence, open has no buffering and fopen has buffering. The former is used in conjunction with read, write, etc., and the latter is used in conjunction with fread, fwrite, etc.

Using the fopen function, since there is a buffer in the user mode, the switching between the user mode and the kernel mode is reduced when performing file read and write operations (switching to the kernel mode call It is still necessary to call the system call API: read, write);

and use the open function, every time when reading and writing files It is necessary to switch between kernel mode and user mode;

shows that if files are accessed sequentially, fopen series of functions are faster than directly calling open series of functions; The opposite is true if the file is accessed randomly.

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of What are the differences between open and fopen in Linux?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!