How to identify open and fopen functions in Linux

  
                

open and fopen are the calling functions of the Linux system. Because they are similar, many people will confuse them. The following is a small series to introduce the difference between open and fopen. Friends who don't understand can come to understand. Features

1. filesystem buffer

is the file system buffer: & ldquo open a memory; buffer & rdquo ;, for each file used in a program, when a read When the file is operated, the data is read from the disk file into the memory "buffer", and then the received variable is read from the memory "buffer"; When performing the operation of writing a file, first write the data to the memory "buffer", and wait until the memory is "filled" and then write the file. It can be seen that the size of the memory "buffer" affects the number of actual operation of external memory. The larger the memory "buffer" is, the less the operation is performed, the faster the execution speed and the higher the efficiency. high. In general, the size of the file "buffer" is determined by the randomizer.

fopen, fclose, fread, fwrite, fgetc, fgets, fputc, fputs, freopen, fseek, ftell, rewind, etc.

2.Unbuffered file system

Buffered files The system manages files by means of file structure pointers, and accesses files through file pointers. It can read and write characters, strings, formatted data, and read and write binary data. The unbuffered file system relies on the operating system to read and write files through the functions of the operating system. It is a system-level input and output. It does not have a file structure pointer, and can only read and write binary files, but it is efficient and fast. The ANSI standard no longer includes unbuffered file systems, so it is recommended that you not choose it. This book is only for a brief introduction. Open, close, read, write, getc, getchar, putc, putchar, etc.

open is a system call. The file handle is returned. The file handle is the index of the file in the file description subtable. fopen is the C library function, and returns a pointer to the file structure.

fopen is a C language library function in the ANSIC standard. Different kernel apis should be called in different systems.

The system functions in linux are open, and fopen is its wrapper function. . for reference only.

File descriptors are a concept under Linux. All devices under Linux operate as files. Such as network sockets, hardware devices, and so on. Of course, including the operation file.

fopen is a standard c function. Return the file stream instead of the file handle under linux.

Device files can not be used as streaming files, only open

fopen is used to manipulate regular files, and has a buffer, there are some differences with open

Generally open the ordinary file with fopen, open the device file with open

fopen is the standard c, and open is the linux system call.

Their levels are different.

fopen portable, open can not

3. Performance

I think the main difference between fopen and open is that fopen has a cache in the user mode, in the read And write time reduces the switching between user mode and kernel mode, while open needs to switch between kernel mode and user mode every time;

behaves as:

(1) if order Accessing files, the function of the fopen series is faster than calling the open series directly;

(2) If the random access file is open faster than fopen.

Classic answer:

The former is a low-level IO, the latter is an advanced IO.

The former returns a file descriptor (in the user program area), which returns a file pointer.

The former is unbuffered and the latter is buffered.

The former works with read, write, etc., and the latter works with fread, fwrite, and so on.

The latter is based on the former, and in most cases, the latter.

The above is the difference between open and fopen, the difference between the two is mainly the difference between buffers, fopen has buffer and open no, and their level is also different, fopen can be transplanted and open can not.

Copyright © Windows knowledge All Rights Reserved