在Linux系统当中,一切都是“文件”:普通文件,驱动程序,网络通信等等。
所有的操作,都是以“文件IO”来进行的,所以啊,系统的学习一下文件io的常用函数是很有必要的。
首先,解决一个问题,文件从哪里来?
一般的来源有3个方面:磁盘,Flash,SD,U盘,硬件的存储媒介中来。
内核提供的虚拟文件系统。5
特殊文件:dev/xxx 上面的设备节点。
那么我们怎么才能访问这些文件呢?
通过通用的IO模型:open/read/write/lseek/close
函数介绍就来了:可以使用man 手册进行函数的查阅。
这里将open/read/write,重点拿出来分析解释。
open函数包含的头文件如下
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
open函数的定义:
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
返回值返回到file descriptor(文件描述符int类型)or -1,如果发生错误的话。
参数pathname:文件名,ep: 1.text ,Auqin.text。
flag:The argument flags must include one of the following access modes: O_RDONLY, O_WRONLY, or O_RDWR.
These request opening the file read-only, write-only, or read/write, respectively.
除此flag之外还包括:file creation flags,The file status flags,这里就不具体展开了,详细可以参考命令man 2 open。
这些参数设定了,正在打开的这个文件的可操作权限,只读,只写,读写权限。应该和mode 进行区分。
mode:The mode argument specifies the file mode bits be applied when a new file is created. This argument must be supplied when O_CREAT or O_TMPFILE is specified in flags; if neither O_CREAT nor O_TMPFILE is specified, then mode is ignored. The effective mode is modified by the process's umask in the usual way: in the absence of a default ACL, the mode of the created file is (mode & ~umask). Note that this mode applies only to future accesses of the newly created file; the open() call that creates a read-only file may well return a read/write file descriptor.
The following symbolic constants are provided for mode:
S_IRWXU 00700 user (file owner) has read, write, and execute permission
S_IRUSR 00400 user has read permission
S_IWUSR 00200 user has write permission
S_IXUSR 00100 user has execute permission
S_IRWXG 00070 group has read, write, and execute permission
S_IRGRP 00040 group has read permission
S_IWGRP 00020 group has write permission
S_IXGRP 00010 group has execute permission
S_IRWXO 00007 others have read, write, and execute permission
S_IROTH 00004 others have read permission
S_IWOTH 00002 others have write permission
S_IXOTH 00001 others have execute permission
特别说明,这个mode 参数只有在创建一个新文件的时候才会生效,具体是针对创建的新文件进行所有者,用户群组,其他群组的读写可行性权限进行约定
即也就是只有在flag包含 O_CREAT or O_TMPFILE的时候,mode才会生效。
read头文件包含如下:
#include <unistd.h>
read函数的定义:
ssize_t read(int fd, void *buf, size_t count);
解释: read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.
关于返回值ssize_t:On success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this number. It is not an error if this number is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of-file, or because we are reading from a pipe, or from a terminal), or because read() was interrupted by a signal. See also NOTES.
On error, -1 is returned, and errno is set appropriately. In this case, it is left unspecified whether the file position (if any) changes.
更加详细的内容可以参考man手册,使用命令man open
剩下的函数write/lseek/close,都是如此,就不进行详细的描述了,具体的可以查看man 手册。