Fopen/openDifferences In UNIX, C has two sets of reading and writing binary stream files: 1) fopen, fread, fwrite; 2) open, read, write Here is a brief introduction of their differences. 1. The fopen series are standard C library functions, and the open series are defined by POSIX and are system calls in UNIX systems. That is to say, the fopen series are more portable, while the open series can only be used in POSIX operating systems. 2. when using fopen functions, you must define an object that refers to a file. It is called "file handler" and is a struct; the open series uses an int integer called "file descriptor. 3. The fopen series are high-level I/O, and buffer is used for reading and writing. The open series are relatively low-level, closer to the operating system, and there is no buffer for reading and writing. Because they can deal with more operating systems, the open series can access and change some information that cannot be accessed by the fopen series, such as viewing the read and write permissions of files. These additional features are generally system-specific. 4. "# include <sdtio. h> "; to use open functions, you need to" # include <fcntl. h> ", use libc (-lc) for Link) Summary: In general, in order to make the program more portable, The fopen series is the first choice without having to use features that cannot be implemented by the fopen series. Read/writeAndFread/fwriteDifferences 1. fread is buffered, and read is not buffered. 2. fopen is defined in standard c, and open is defined in POSIX. 3. fread can read a structure. There is no difference between reading binary files in linux/unix. 4. fopen cannot specify the permission to create a file. open can specify the permission. 5. fopen returns the pointer and open returns the file descriptor (integer ). 6. If any device in linux/unix is a file, you can use open or read. If the file size is 8 k. 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 descriptors, use read and write, such as a set of interfaces and pipelines. The efficiency of calling write by the system depends on the size of your buf and the total number of writes you want to write. If the buf is too small, the number of times you enter the kernel space increases greatly, resulting in low efficiency. Fwrite caches the data for you to reduce the number of system calls that actually occur, so the efficiency is relatively high. If it is only called once (is it possible ?), The two are similar. Strictly speaking, write is a little faster (because fwrite actually uses write to write the file system), but the difference does not matter. |