Linux Read/write Fread/fwrite The difference between the two

Source: Internet
Author: User
Tags define function fread posix

Linux Read/write Fread/fwrite The difference between the two

The 1,fread is buffered and read without buffering.

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

3,fread can read a structure. Read in Linux/unix There is no difference between reading binary and normal files.

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

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

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

If the size of the file is 8k.

If you use Read/write and only allocate 2k of cache, you need to make 4 system calls to read this file to actually read from disk.

If you use Fread/fwrite, the system automatically allocates the cache, and the read out of this file is read out of the disk as soon as the system call is made.

That is, with Read/write to read 4 disks, and Fread/fwrite, as long as read 1 times the disk, the efficiency is 4 times times higher than the read/write.

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

With Fread and fwrite, it automatically allocates caches, which can be very fast and simpler than what you do. if you want to handle some special descriptors, use read and write, such as socket, pipe, etc.

The efficiency of the system call write is determined by the size of your buf and the total number of writes you want to write, and if the buf is too small, you get into the kernel space a lot and your efficiency is low.

And fwrite will cache for you, reducing the actual system calls, so the efficiency is higher.

If it is called only once (possibly?), the two are almost, strictly speaking, a little bit faster (because in fact fwrite finally used write to do the real writing file system work)

Open (opens file)

Correlation function Read,write,fcntl,close,link,stat,umask,unlink,fopen

Table header File

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

Defining functions

int open (const char * pathname, int flags);

int open (const char * pathname,int flags, mode_t mode);

The function description parameter pathname points to the file path string that you want to open.

The following are the flags that the parameter flags can use:

O_rdonly opening a file as read-only

O_wronly opening a file as a write-only method

O_rdwr Open the file in a writable manner, The above three kinds of flags are mutually exclusive, that is, cannot be used simultaneously, but can be used with the following flags or (|) operator combinations.

O_creat If the file you want to open does not exist, the file is created automatically.

O_excl If O_creat is also set, this command checks to see if the file exists. The file is created if it does not exist, otherwise it will cause an open file error. In addition, if O_creat and O_EXCL are set simultaneously and the file to be opened is a symbolic connection, the file fails to open.

O_noctty If the file you want to open is a terminal device, the terminal is not treated as a Process control terminal.

O_trunc if the file exists and opens in a writable manner, this flag bidding clubs the file length to 0, and the data originally stored in the file disappears.

O_append when a file is read and written, it moves from the end of the file, which means that the data is appended to the file after it is added.

The O_nonblock opens the file in an unstoppable way, that is, whether or not data is read or waiting, it will be returned to the process immediately.

O_ndelay with O_nonblock.

O_sync Open the file in a synchronized manner.

O_nofollow if the file referred to by the parameter pathname is a symbolic connection, the open file will fail.

O_directory if the file that the parameter pathname refers to is not a directory, the open file fails. This is a special flag after Linux2.2 to avoid some system security problems.

Parameter mode has the following combinations, which will only take effect when a new file is created, and the permissions of the real file will be affected by the umask value, so the file permission should be (Mode-umaks)

S_irwxu 00700 permission that represents the file owner with readable, writable, and executable permissions.

S_IRUSR or s_iread,00400 permission that represents the owner of the file with readable permissions.

S_IWUSR or S_IWRITE,00200 permission that represents the file owner with writable permissions.

A s_ixusr or S_IEXEC,00100 permission that represents the owner of the file that has executable permissions.

S_IRWXG 00070 permission, which represents the file user group with readable, writable, and executable permissions.

S_IRGRP 00040 permission, which represents the file user group with readable permissions.

S_IWGRP 00020 permission, which represents the file user group with writable permissions.

S_IXGRP 00010 permissions, which represent the permissions that the file user group has to perform.

S_irwxo 00007 permissions, which represent other users with readable, writable, and executable permissions.

S_iroth 00004 permissions, which represent other users with readable permissions.

S_iwoth 00002 permissions, which represent other users with writable permissions.

S_ixoth 00001 permissions, which represent other users with enforceable permissions.

return value:

If all the permissions that you want to check are checked, a value of 0 is returned, indicating success. Returns 1 if one of the permissions is disabled.

Error code

The eexist parameter pathname refers to a file that already exists, but uses the o_creat and O_EXCL flags.

The eaccess parameter pathname refers to a file that does not meet the required test permissions.

Erofs files to test write permissions exist in the read-only file system.

The Efault parameter pathname pointer is out of the accessible memory space.

The EINVAL parameter mode is incorrect.

Enametoolong parameter pathname too long.

The Enotdir parameter pathname is not a directory.

Enomem core memory is low.

The Eloop parameter pathname has too many symbolic connection problems.

EIO I/O access error.

Read (reading data from an open file) related functions Readdir,write,fcntl,close,lseek,readlink,fread

Table header file #include <unistd.h>

Defines the function ssize_t read (int fd,void * buf, size_t count);

Function Description:

Read () transfers the file referred to by the parameter fd to count bytes to the memory referred to by the BUF pointer. If the parameter count is 0. Then read () will not function and return 0. The return value is the number of bytes actually read, if 0 is returned. Indicates that the end of the file has been reached or that there is no readable data. In addition, the file read and write locations are moved with the bytes read.


Additional Instructions:

If a smooth read () returns the number of bytes actually read, it is better to compare the return value to the parameter count, and if the number of bytes returned is less than the number of bytes required to read, it is possible to read the end of the file, read from the pipe (pipe) or terminal, or by reading () The read action is interrupted by the signal. Returns 1 when an error occurs, the error code is stored in errno, and the file read and write location cannot be expected.


Error code:

Eintr This call is interrupted by the signal.

Eagain returns this value when non-blocking I/O is used (o_nonblock) if no data is available to read.

EBADF parameter FD is not a valid file descriptor, or the file is closed.



Sync (write buffer data back to disk)

Correlation function Fsync

Table header file #include <unistd.h>

define function int sync (void)

Function Description:

Sync () is responsible for writing the system buffer data back to disk to ensure data synchronization.

Return value: returns 0.

Write (writes data to an open file) correlation function Open,read,fcntl,close,lseek,sync,fsync,fwrite

Table header file #include <unistd.h>

Defines the function ssize_t write (int fd,const void * buf,size_t count);

Function Description:

Write () writes the memory referred to by the parameter buf to count bytes to the file referred to by the parameter fd, and, of course, the file read and write location moves with it.

return value:

If smooth write () returns the number of bytes actually written, 1 is returned when an error occurs, and the error code is stored in errno.

Error code:

Eintr This call is interrupted by the signal.

Eagain when non-blocking I/O is used (o_nonblock), this value is returned if no data is readable.

EADF parameter FD is not a valid file descriptor, or the file is closed.

fopen (open file) related functions Open,fclose

Table header file #include <stdio.h>

Defines the function FILE * fopen (const char * path,const char * mode);

Function description The path string contains the file path and filename to open, and the parameter mode string represents the flow pattern.

Mode has the following pattern strings:

R opens a read-only file that must exist. r+ open a writable file that must exist.

W Open Write-only file, if the file exists then the file length is clear to 0, that is, the contents of the file will disappear. If the file does not exist, the file is created.

w+ Open a read-write file, if the file exists, the file length is clear to 0. That is, the file content disappears. If the file does not exist, the file is created.

A write-only file is opened in an additional way. If the file does not exist, the file will be created, and if the file exists, the data written will be appended to the end of the file, that is, the original content of the file will be retained.

A + opens a read-write file in an additional way. If the file does not exist, the file will be created, and if the file exists, the data written will be appended to the end of the file, that is, the original content of the file will be retained.

All of the above morphological strings can be added with a B character, such as rb/w+b/ab+, and a B character is used to tell the library that the file opened is a binary file, not a plain text file.

However, in a POSIX system, including Linux ignores the character.

A new file created by fopen () will have a s_irusr| s_iwusr| s_irgrp| s_iwgrp| s_iroth| S_iwoth (0666) permissions. This file permission also references the umask value.

return value:

When the file is opened successfully, the file pointer to the stream is returned.

If the file fails to open, it returns null and the error code exists in errno.

Additional instructions in general, after opening the file will make some file read or write action, if the file fails, the next read and write action can not be carried out smoothly, so after fopen () Please make error judgment and processing.

Linux read/write fread/fwrite difference

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.