文档详情

Linux C函数速查-Linux系统编程读书笔记.pdf

发布:2017-09-22约2.48万字共31页下载文档
文本预览下载声明
Linux C函数速查-Linux系统编程读书笔记 《Linux系统编程》读书笔记 第第2章章 文文件件IO 2.1打打开开文文件件 2.1.1 系统调用open() #include sys/types .h #include sys/stat .h #include fcntl.h int open(const char *name, int fla s); int open(const char *name, int fla s, mode_t mode); fla s参数:O_RDONLY 、O_W RONLY 、O_RDW R、O_A PPEND、O_CREAT 、O_T RUNC等。 2.2 通通过过read()读读文文件件 #include unistd.h ssize_t read(int fd, void *buf, size_t len); 2.3 通通过过write()写写文文件件 #include unistd.h ssize_t write(int fd, const void *buf, size_t count); 2.4 同同步步IO 2.4.1 fsync()和fdatasync() #include unistd.h int fsync(int fd); int fdatasync(int fd); 区 :fsync()确保数据和元数据写到磁盘;fdatasync()保证以后访问文件所需要的元数据写到磁盘 (如 文件大小),其他非基础的不保证。 /p/rongxh7 Linux C函数速查-Linux系统编程读书笔记 2.4.2 sync() #include unistd.h void sync(void); 2.6 关关闭闭文文件件 #include unistd.h int close(int fd); 2.7 用用lseek()查查找找 #include sys/types #include unistd.h off_t lseek(int fd, off_t pos, int ori in); ori in参数:SEEK_CUR、SEEK_END、SEEK_SET pos参数:偏移量 (正、0、负) 2.8 定定位位读读写写 #define _XOPEN_SOURCE 500 #include unistd.h ssize_t pread(int fd, void *buf, size_t count, off_t pos); sszie_t pwrite(int fd, const void *buf, size_t count, off_t pos); 对比:pread()和pwrite()避免了在使用lseek()是会出现的竞争。 2.9 文文件件截截断断 #include unistd.h #include sys/types .h int ftruncate(int fd, off_t len); /p/rongxh7 Linux C函数速查-Linux系统编程读书笔记 int truncate(const char *path, off_t len); 2.10 IO多多路路复复用用 2.10.1 select() #include sys/select .h int select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); 参数n:所有集合中最大的fd值加1,如ST DIN_FILENO+1,ST DOUT_FILENO+1。 返回值:返回三个集合中I/O就绪的文件描述符总数,超时返回0,出错返回-1。 FD_CLR(int fd, fd_set *set); FD_ISSET (int fd, fd_set *set); FD_SET (int fd, fd_set *set); FD_ZERO(fd_set *set); #include
显示全部
相似文档