|
所需头文件:#include
函数原型:int close (int fd )
函数输入值:fd :文件描述符
函数返回值:成功:0 出错:-1
3 、Read 函数语法要点
所需头文件:#include
函数原型:ssize_t read(int fd,void *buf,size_t count)
函数传入值
fd: 文件描述符
Buf :指定存储器读出数据的缓冲区
Count :指定读出的字节数
函数返回值:成功:读出的字节数 0 :已到达文件尾 -1 :出错
在读普通文件时,若读到要求的字节数之前已达到文件的尾部,则返回字节数会小于希望读出的字节数。
4 、Write 函数语法要点
所需头文件:#include
函数原型: ssize_t write(int fd,void *buf,size_t count)
函数传入值:
fd: 文件描述符
Buf :指定存储器写入数据的缓冲区
Count :指定读出的字节数
函数返回值:成功:已写的字节数 -1 :出错
5 、Lseek 函数语法要点:
所需头文件:#include
#include
函数原型:off_t lseek(int fd,off_t offset,int whence)
函数传入值:
fd: 文件描述符
Offset :偏移量,每一读写操作所需要移动的距离,单位是字节的数量,可正可负(向前移,向后移)
Whence :当前位置的基点:
SEEK_SET :当前位置为文件开头,新位置为偏移量的大小
SEEK_CUR :当前位置为文件指针位置,新位置为当前位置加上偏移量
SEEK_END :当前位置为文件的结尾,新位置为文件的大小加上偏移量大小
函数使用实例:
/**/
#include
#include
#include
#include
#include
#include
#include
int main(void)
{
char *buf="Hello! I'm writing to this file!";
char buf_r[11];
int fd,size,len;
len = strlen(buf);
buf_r[10] = '/0';
/* 首先调用open 函数,并指定相应的权限*/
if ((fd = open("hello.c", O_CREAT | O_TRUNC | O_RDWR,0666 )) |
|
|