Creat函数
表头文件:#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
定义函数: int creat(const char *pathname,mode_tmode);
函数说明: 参数pathname指向欲建立的文件路径字符串,creat()相当于使用下列的调用方式调用open(); open(const char *pathname,(O_CREAT|O_WRONLY|O_TRUNC));
Open函数
表头文件:#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
定义函数:int open( const char * pathname, int flags);
int open( const char * pathname,int flags, mode_t mode);
Read函数
表头文件: #include<unistd.h>
定义函数:ssize_t read(int fd,void * buf ,size_t count);
函数说明: read( )会把参数fd所指的文件传送count个字节到buf指针所指的内存中。若参数count为0,则read( )不会有作用并返回0。返回值为实际读取到的字节数,如果返回0,表示已到达文件尾或是无可读取的数据,此外文件读写位置会随读取到的字节移动
Write函数
表头文件: #include<unistd.h>
定义函数: ssize_t write (int fd,const void * buf,size_t count);
函数说明: write( )会把参数buf所指的内存写入count个字节到参数fd所指的文件内。当然,文件读写位置也会随之移动
Lseek函数
表头文件: #include<sys/types.h>
#include<unistd.h>
定义函数: off_t lseek(int fildes,off_t offset ,int whence);
函数说明: 每一个已打开的文件都有一个读写位置,在打开文件时通常其读写位置指向文件开头,若是以附加的方式打开文件(如O_APPEND),则读写位置会指向文件尾。当read( ) 或write( )时,读写位置会随之增加,lseek( )便是用来控制该文件的读写位置的。参数fildes为已打开的文件描述词;参数offset为根据参数whence来移动读写位置的位移数,参数whence为下列其中一种。
l SEEK_SET:参数offset即为新的读写位置。
l SEEK_CUR:以目前的读写位置往后增加offset个位移量。
l SEEK_END:将读写位置指向文件尾后再增加offset个位移量。
当whence值为SEEK_CUR或SEEK_END时,参数offet允许负值的出现。下列是较特别的使用方式:
l 欲将读写位置移到文件开头时,lseek(int fildes,0,SEEK_SET)。
l 欲将读写位置移到文件尾时,lseek(int fildes,0,SEEK_END)。
l 想要取得目前文件位置时,lseek(int fildes,0,SEEK_CUR)
Close函数
表头文件:#include<stdio.h>
定义函数:int close(int fd)
函数说明:close( )用来关闭open( )打开的文件