Linux under Fread/read and Fwrite/write__linux

Source: Internet
Author: User
Tags fread

The 1,fread is buffered with read without buffering.

2,fopen is defined in standard C, and open is defined in POSIX.

3,fread can read a structure. Read the binary in Linux/unix is no different from normal files.

4,fopen cannot specify permissions to create a file. Open can specify permissions.

5,fopen returns the pointer, open returns the file descriptor (integer).

Any device in the 6,linux/unix is a file and can be used open,read.

If the file size is 8k.

If you use Read/write, and only 2k of cache is allocated, you need to read the file 4 times to actually read it from the disk.

If you use Fread/fwrite, the system automatically allocates the cache, then the file is read out once the system call is read from the disk.

That is, use Read/write to read Disk 4 times, and use fread/fwrite only read 1 times disk. Efficiency is 4 times times higher than Read/write.

If the program has limitations internally, it is better to use Read/write.

With Fread and fwrite, it automatically allocates caching, which is faster and simpler than what you do. If you want to handle some special descriptors, use read and write, such as a socket, pipe, etc.

The efficiency of the system call write depends on the size of your buf and the total number of writes you have to write, and if the buf is too small, the number of times you enter the kernel space increases significantly and the efficiency is low. And fwrite will cache for you, reducing the actual system calls, so the efficiency is high.

If you only call once (possibly?), the two are pretty much the same, and it's a little bit faster to write (because in fact, Fwrite finally used write to do the actual file system work), but the difference doesn't matter.

============  The Read/write system calls the Read function to read data from an open device or file.

#include <unistd.h>  ssize_t Read (int fd, void *buf, size_t count); return value: Returns the number of bytes read successfully, error returns-1 and sets the errno, If the end of the file is reached before the read, the read returns 0
parameter count is the number of bytes requested to read, the read data is saved in the buffer buf, and the file's current read-write position is moved backwards. Note that this read-write location may be different from the read/write location when using the C standard I/O library, which is recorded in the kernel, while the read-write location when using the C-standard I/O library is the location in the user-space I/O buffer.
Fread is implemented through read, Fread is a library of C, and read is a system call
but the difference is that in read each read is the size of the caller's request, for example, when the call requires reading 10 bytes of data, read reads 10 bytes of data into the array, And Fread is different, in order to speed up the speed of reading, the fread will read more data each time, and then put into the buffer, so that the next time you read the data only need to go to the buffer to fetch it.

Fread reads one buffer size at a time, 32-bit is typically 4,096 bytes, equivalent to a read (fd,buf,4096)

, such as reading 512 bytes of data, 4 reads, and call read:
For (i=0 i<4; ++i)
Read (fd,buf,128)
A total of 4 system calls

and fread read 4096 bytes at a time to put in the buffer, so it's easy to save the

For example, read a BYTE, fgetc may read 1024 bytes from the kernel to the I/O buffer and then return the first byte, where the read and write position recorded in the kernel is 1024, while the read-write position recorded in the file structure is 1. Note The return value type is ssize_t, representing a signed size_t that can return a positive number of bytes, 0 (indicating the end of the file) or negative-1 (indicates an error). When the Read function returns, the return value shows how many bytes before the buf are just read. In some cases, the actual number of bytes read (the return value) is less than the count of bytes requested for reading, for example,

When reading a regular file, the end of the file is reached before count bytes are read. For example, 100 bytes are requested to be read at the end of the file, then read returns 30, and the next read returns 0. The

is read from a terminal device, usually in a behavior unit, and is returned by reading to a newline character.

Read from the network, depending on the transport layer protocol and kernel caching mechanism, the return value may be less than the requested number of bytes, which is explained in detail later in the Socket Programming section.

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.