The difference between fopen and open

Source: Internet
Author: User
Tags fread

The difference between open and fopen:

1. Buffered File system
The buffer file system is characterized by a "buffer" in memory, for each file in the program, when performing the operation of the read file, the data is read into the memory "buffer" from the disk file, loaded and then read into the received variable from the memory "buffer". 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, rewind, etc.
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. This book is for brief introduction only. Open, close, read, write, getc, GetChar, PUTC, Putchar, etc.

Open is the file handle returned by the system call, and the file's handle is the index of the file in the file description list, fopen is the library function of C, and returns a pointer to the file structure.

Fopen is a C library function in the Ansic standard, and different kernel APIs should be called in different systems.
The system function in Linux is Open,fopen is its encapsulation function, personal point of view. For reference only.

File descriptors are a concept under Linux, and everything under Linux is in the form of files, such as network sockets, hardware devices, and so on. The operation files are of course included.
Fopen is a standard C function. Returns the file stream instead of the file handle under Linux.

Device files can not be used as streaming files, can only use the open
Fopen is used to manipulate the regular file, and there is a buffer, with open or some difference

General use fopen Open the ordinary file, open the device file with open

Fopen is in standard C, and open is a system call to Linux.
Their levels are different.
fopen Portable, open cannot be

I think the main difference between fopen and open is that fopen has a cache in the user state, which reduces the switching of the user state and the kernel state when the read and write are performed, while open switches the kernel state and the user state each time, and behaves as if the file is accessed sequentially, The functions of the Fopen series are faster than calling the Open series directly, and if the file is accessed randomly, the open is faster than fopen.

Classic answers from the forum:

The former belongs to low-level IO, which is advanced IO.
The former returns a file descriptor (the user program area), which returns a file pointer.
The former has no buffering and the latter is buffered.
The former and read, write and so on with the use of the latter with Fread, fwrite and so on with the use.
The latter is based on the former, in most cases, with the latter.

The difference between fopen and open

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.