Differences between open/read/write and fopen/fread/fwrite

Source: Internet
Author: User
Differences between open/read/write and fopen/fread/fwrite

 

Open: system call. The returned file descriptor, that is, the file handle, is the index of the file in the file description table.

Fopen: a c language library function that returns a pointer to the file structure. Fopen is a C language library function in ansi c. different operating systems should call Different kernel APIs. in UNIX environments, fopen is an encapsulation of open.

File descriptor is a concept in Unix/Linux. in Linux, all devices are files, and all devices are operated in the form of files, such as network sockets and hardware devices. For the differences between file descriptors and file pointers, see the blog: differences between file descriptors and file pointers.

Device Files cannot be processed as stream files. Therefore, only open files can be used, while fopen files are used to manipulate regular files and set to buffer files, which is different from open files.

 

The difference between open and fopen is:

 

1. Buffer File System

The buffer file system manages files by means of the file structure pointer file *, and accesses the files through the file pointer, which can read and write characters, strings, formatted data, or binary data.

Buffer file system features: a "buffer" is opened in the memory for each file in the program. When reading a file, the data is first read into the memory "buffer" from the disk file ", then, the received variables are read from the memory buffer. When the file write operation is performed, data is first written to the memory "buffer", and then written to the file after the memory "buffer" is filled up. From this we can see that the size of the memory "buffer" affects the number of external operations. The larger the memory "buffer", the smaller the number of external operations and the faster the execution speed, the higher the efficiency. Generally, the size of the file "buffer" is related to the machine.

Io functions of the buffer file system mainly include fopen, fclose, fread, fwrite, fgetc, fgets, fputc, fputs, freopen, fseek, ftell, and rewind.

 

2. Non-buffered File System

The non-buffered file system depends on the operating system. It reads and writes files through the operating system function. It is a system-level input and output. It does not have a file structure pointer, only binary files can be read and written (for the UNIX system kernel, there is no difference between text files and binary code files), but the efficiency is high and the speed is fast, because the ANSI standard does not include non-buffer file systems, therefore, when reading a regular file, it is recommended that you do not select it.

Io functions of the non-buffer file system mainly include: open, close, read, write, GETC, getchar, putc, and putchar.

 

For example, the efficiency of open and fopen functions is illustrated: if the file size is 8 KB.

If you use read/write and only allocate 2 k cache, You need to perform four system calls to read the file from the disk. If you use fread/fwrite, the system automatically allocates the cache and reads the file as long as one system call is read from the disk. That is, read/write is used to read the disk four times, while fread/fwrite is used to read the disk only once. It is 4 times more efficient than read/write. If the program has a limit on the memory, it is better to use read/write. Fread and fwrite are used to automatically allocate the cache, which is faster and easier than doing it on your own. If you want to process some special files, use read and write, such as a set of interfaces and device files such as pipelines.

The efficiency of system calling write depends on the size of your buffer and the total number of writes you want. If the buffer is too small, the number of times you enter the kernel space increases greatly, and the efficiency is low. Fwrite caches the data for you to reduce the number of system calls that actually occur, so the efficiency is relatively high.

If only one call is required (this is a relatively small possibility), the two are similar. Strictly speaking, write is a little faster, because fwrite encapsulates write, in the end, write is used to write the file system, but the difference does not matter.

 

The main difference between open and fopen is that fopen has a cache in the user mode. During read and write operations, switching between the user mode and the kernel mode is reduced, while open requires kernel mode and user mode switching every time. It shows that, if files are accessed sequentially, fopen system functions are faster than directly calling open system functions, if files are randomly accessed, open functions are faster than fopen functions.

 

Therefore, the differences between open series functions and fopen series functions can be summarized as follows:

Open Functions

Fopen Functions

It is generally used to open a device file (in rare cases)

It is generally used to open common files (in most cases)

Manipulate a file using a file descriptor

Operate a file using a file pointer

Open returns a file descriptor.

Fopen returns a file pointer.

POSIX system call

Ansi c library functions

Low-level Io

High-level Io, expansion and encapsulation of open

It can only be ported to the POSIX Operating System

Can be transplanted to any operating system

Non-buffered Io

Buffer Io

Only binary or plain text can be read.

A structure can be read.

You can specify the access permission for the file to be created.

You cannot specify the access permission for the file to be created.

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.