In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "what is the difference between open and fopen in Linux". The content in the article is simple and clear, easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the difference between open and fopen in Linux".
Differences: 1, open is the UNIX system call function, while fopen is the C language library function in the ANSIC standard; 2, the portability of open is not as good as fopen; 3, fopen can only operate ordinary files, while open can operate ordinary files, network sockets, etc.; 4, open is not buffered, fopen is buffered.
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 point of view of the source, the two can be well distinguished, which is the most obvious difference between the two:
Open is the UNIX system call function (including LINUX, etc.), which returns the file descriptor (fdescripd), which is the index of the file in the file descriptor table.
Fopen is a C language library function in the ANSIC standard, and different kernel api should be called in different systems. A pointer to the file structure is returned.
2. Portability
It can be inferred from the above source that `fopen` is a C standard function, so it has good portability, while `open` is a UNIX system call and its portability is limited. For example, similar features in windows use the API function `CreateFile`.
3. Scope of application
Open returns file descriptors, and file descriptors are an important concept in UNIX systems. All devices under UNIX operate in the form of files. Such as network sockets, hardware devices and so on. Of course, it includes operating ordinary regular files (Regular File).
Fopen is used to manipulate ordinary regular files (Regular File).
4. File IO hierarchy
From the point of view of the file IO, the former belongs to the low-level IO function and the latter belongs to the high-level IO function. The simple distinction between low-level and high-level is who is closer to the system kernel. The low-level file IO runs in kernel mode, while the high-level file IO runs in user mode.
5. Buffer
Buffer file system
The characteristics of the buffered file system are as follows: a "buffer" is opened in the memory to be used by every file in the program; when the operation of reading the file is performed, the data is first read into the memory "buffer" from the disk file, and then the required data is read out from the memory "buffer" when it is full. When performing the operation of writing to a file, the data is first written to the memory "buffer", and then the file is written after the memory "buffer" is full. From this we can see that the size of the memory "buffer" affects the number of times of the actual operation of external memory. The larger the memory "buffer" is, the less the number of times of operating external memory is, and the execution speed is fast and the efficiency is high. In general, the size of the file "buffer" depends on the machine. Fopen, fclose, fread, fwrite, fgetc, fgets, fputc, fputs, freopen, fseek, ftell, rewind, etc.
Unbuffered file system
The buffer file system manages the file with the help of the file structure pointer, and accesses the file through the file pointer, which can read and write characters, strings, formatted data, and binary data. The non-buffered file system depends on the operating system, which reads and writes files through the function of the operating system, which is system-level input and output. It has no file structure pointer and can only read and write binary files, but it is efficient and fast. Because the ANSI standard no longer includes non-buffered file system, it is recommended that you had better not choose it. Open, close, read, write, getc, getchar, putc, putchar, etc.
To sum up, open is unbuffered and fopen is buffered. The former is used with read, write, etc., and the latter is used with fread,fwrite, etc.
Using the fopen function, because there is a buffer in the user mode, the switching between the user mode and the kernel state is reduced when reading and writing files (the system call API:read,write is still needed to switch to the kernel mode call).
Using the open function, you need to switch between kernel state and user mode every time you read and write a file.
It shows that if the files are accessed sequentially, the functions of the fopen series are faster than directly calling the functions of the open series; if the files are accessed randomly, the opposite is true.
Thank you for your reading, the above is the content of "what is the difference between open and fopen in Linux". After the study of this article, I believe you have a deeper understanding of what is the difference between open and fopen in Linux. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.