linux read / write and fread / fwrite What is the difference

linux read / write and fread / fwrite What is the difference 

1, fread is buffered to, read without a buffer.

2, fopen is standards defined in c , open is defined in POSIX.

3, fread can read a configuration .read in the linux / unix the read binary ordinary file, there is no difference.

4, fopen can not specify the file to create rights .open can assign permissions .

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

6, any device is a file linux / unix in, you can use open, read.

 

If the file size is 8k.

If you use read / write, and allocated only 2k cache, you will have to read out the file needs to be done four times system call to actually read from the disk.

If you use fread / fwrite, the system will automatically assign a cache , then read this file as long as the system call read out from the disk.

That is, a read / write disk to read 4 times, and use fread / fwrite read 1 times as long as the disk. Efficient than the read / write 4 times.

If the program is limited to memory , use read / write better.

Are used fread and fwrite, it is automatically assigned cache, speed quickly , do simpler than their own. To deal with some specific descriptor, and with read Write , such as sockets, pipe or the like

The efficiency of the system call you write depends on the size of buf and the total number you want to write, if buf is too small, the number of times you get into kernel space increase, the efficiency is low. The fwrite will do it for you cache, reducing the actual occurrence of the system call, so the efficiency is relatively high.

If you call only once (possible?), Maybe about the same, strictly speaking, to write a little bit faster (because in fact fwrite finally do write with a real write file system works), but this is where the difference does not matter.

 

the difference between open and fopen: the
former are low-level IO, which is a high-level IO.
The former returns a file descriptor , which returns a file pointer .
The former no buffer, which has a buffer.
The former read, writ e  used in conjunction, with the latter  fread, fwrite used in conjunction.
The latter is the basis of the expansion from the former , and in most cases, by the latter .

 

open (Open File)

Related functions
read, write, fcntl, close,
header file
#include <SYS / types.h>
#include <SYS / defined in stat.h>
#include <fcntl.h>
defined function
int open (const char * pathname, the flags int);
int open (const char * pathname, int the flags, MODE mode_t);
function Description
parameter pathname  points to the file path string to be opened .

 

Parameters flags  :
O_RDONLY open the file in read-only mode
O_WRONLY open the file for writing only
O_RDWR open the file in a read-write. The three flags are mutually exclusive, that is, not both, but may use the following flags OR (|) operator composition.
O_CREAT  Ruoyu open the file does not exist, automatically create the file.
O_EXCL if O_CREAT is also set, this command will check whether a file exists. If files exist, create the file , otherwise it will cause an error to open the file. Further, if the O_CREAT O_EXCL also set, and the file to be opened is a symbolic link will open the file fail.
O_NO CTTY  If you want to open a file for the terminal equipment when is not the terminal as a process control terminal.
O_TRUNC If the file exists and is writable way when open, this flag will make the file length is cleared to 0 , and the original data stored in the file will disappear.
O_APPEND moves from the start end of the file when the file read and write, the data is written will be added to the back of the additional file manner.
O_NON BLOCK  open the file in a non-blocking manner, that is, regardless of whether the data is read or wait, will return immediately into the process.
O_NDELAY with O_NONBLOCK.
O_SYNC open the file in a synchronized manner.
O_NO FOLLOW  file if the pathname argument is referred to a symbolic link , so that it will fail to open the file.
O_DIRECTORY If the parameter file pathname referred not to a directory , it will make open file failed.
This is a unique flag Linux2.2 later, in order to avoid some system security issues.

 

Parameters mode there are following several combinations, it will only take effect when you create a new file , in addition to the real construction permissions files will be affected by the umask value, so the file permissions should be (mode-umaks).
S_IRWXU, 00700 authority, on behalf of the file owner has permission to read, write and executable.
S_IRUSR or S_IREAD, 00400 authority, on behalf of the owner of the file has permissions can read.
S_IWUSR or S_IWRITE, 00200 authority, on behalf of the owner of the file has write permission.
S_IXUSR or S_IEXEC, 00100 authority, on behalf of the owner of the file has executable permissions.
S_IRWXG 00070 permission, the document on behalf of a user group with read, write and execute permissions.
S_IRGRP 00040 permission, the document on behalf of a user group has read permission.
S_IWGRP 00020 authority, on behalf of the user group has permission to file writable.
S_IXGRP 00010 authority, on behalf of the user group has permission to file executable.
S_IRWXO 00007 authority, on behalf of other users have read, write and executable permissions.
S_IROTH 00004 permission behalf of other users have read privileges
S_IWOTH 00002 permission on behalf of other users with write permission.
S_IXOTH 00001 authority, on behalf of other users have executable permissions.

The return value
if all permissions have to be verified by checking the return value is 0, indicating success, as long as there is a permission is prohibited or -1.
Error code
file pathname parameter EEXIST referred to already exists, it does use the O_CREAT and O_EXCL flag.
EACCESS parameter file pathname does not comply rights within the meaning of the required tests.
EROFS To test write access to a file exists in the read-only file system.
EFAULT pathname argument pointer memory accessible beyond.
EINVAL mode parameter is incorrect.
ENAMETOOLONG parameters pathname is too long.
ENOTDIR argument pathname is not a directory.
ENOMEM Insufficient memory core.
ELOOP pathname parameter has too many symbolic links problem.
EIO I / O access error.

 

Additional instructions access () to determine the certification for users to be especially careful, for example, and then open the access () () empty file can cause problems on your system security.

example

#include <unistd.h>
#include <SYS / types.h>
#include <SYS / defined in stat.h>
#include <fcntl.h>
main ()
{
int FD, size;
char S [] = "the Linux Programmer! \ n-", Buffer [80];
FD = Open (" / tmp / TEMP ", O_WRONLY | the O_CREAT);
Write (FD, S, the sizeof (S));
Close (FD);
FD = Open (" / tmp / TEMP ", of O_RDONLY);
size = read (FD, Buffer, the sizeof (Buffer));
Close (FD);
the printf ("% S ", Buffer);
}

! Programmer perform the Linux

read (read data from the opened file )
correlation function readdir, write, fcntl, close, lseek, readlink, fread
header file #include <unistd.h>
defined functions

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

Function Description read () parameter file will fd referred to memory transfer count bytes in buf pointer. If the parameter count is 0, the read () does not function and returns 0. The return value is the number of bytes actually read, returns 0 if, no data or indicates the end of the file has been reached the read, write location of the file in addition be moved along with the read byte.

Additional information if successful read () will return the actual read the number of bytes , is best able to return the value of the parameter count  for comparison , if the number of bytes less the number of bytes read is returned than required, it is possible to read the end of file , or read from the terminal duct (pipe), or a read () is interrupted by a signal reading operation. When an error occurs -1 is returned, the error code is stored in errno, but the location can not be expected to read and write files.

The error code EINTR This call was interrupted by a signal.
When using a non-blocking EAGAIN when I / O (O_NONBLOCK), if no data can read this value is returned.
EBADF fd parameter non-valid file descriptor, or the file is closed.

Examples of reference open ().

write (data is written to the file open)
correlation function open, read, fcntl, close, lseek, sync, fsync, fwrite
header file #include <unistd.h>
Defined Functions

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


函数说明 write()会把参数buf所指的内存写入count个字节到参数fd所指的文件内。当然,文件读写位置也会随之移动。
返回值 如果顺利write()会返回实际写入的字节数。当有错误发生时则返回-1,错误代码存入errno中。
错误代码 EINTR 此调用被信号所中断。
EAGAIN 当使用不可阻断I/O 时(O_NONBLOCK),若无数据可读取则返回此值。
EADF 参数fd非有效的文件描述词,或该文件已关闭。
范例 请参考open()。

 

sync(将缓冲区数据写回磁盘)
相关函数 fsync
表头文件 #include<unistd.h>
定义函数 int sync(void)
函数说明 sync()负责将系统缓冲区数据写回磁盘,以确保数据同步。
返回值 返回0。


fopen(打开文件)
相关函数
open,fclose
表头文件
#include<stdio.h>
定义函数
FILE * fopen(const char * path,const char * mode);
函数说明
参数path字符串包含欲打开的文件路径及文件名,参数mode字符串则代表着流形态。
mode有下列几种形态字符串:
r 打开只读文件,该文件必须存在
r+ 打开可读写的文件,该文件必须存在
w 打开只写文件,若文件存在则文件长度清为0,即该文件内容会消失。若文件不存在则建立该文件
w+ 打开可读写文件,若文件存在则文件长度清为零,即该文件内容会消失。若文件不存在则建立该文件
a 以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留
a+ 以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。
上述的形态字符串都可以再加一个b字符,如rb、w+b或ab+等组合,加入b 字符用来告诉函数库打开的文件为二进制文件,而非纯文字文件。不过在POSIX系统,包含Linux都会忽略该字符。由fopen()所建立的新文件会具 有S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH(0666)权限,此文件权限也会参考umask 值。

返回值
文件顺利打开后,指向该文件指针就会被返回。若果文件打开失败则返回NULL,并把错误代码存在errno 中。
附加说明
一般而言,打开文件后会作一些文件读取或写入的动作,若开文件失败,接下来的读写动作也无法顺利进行,所以在fopen()后请作错误判断及处理。


 

 

Guess you like

Origin blog.csdn.net/qq_38971487/article/details/91492982