int fd;
fd = open(filename);
lseek(fd,offset,whence);
write(fd,buff,write_len);
read(fd,buff,read_len);
close(fd);
特殊的索引
实际上是Linux系统下进程的file_struct结构体下的file_array数组下标,数组成员为file_operation操作结构体。
模式
主模式:
主模式只能有一个
副模式可以使用 | 运算符指定多个
包含的头文件
#include
#include
#incude
函数原型
int open(const char* pathname,int flags)
int open(const char* pathname,int flags,int perms)
返回值:
包含的头文件
#include
函数原型
int close(int fd);
返回值
包含的头文件
#include
函数原型
ssize_t read(int fd,void*buff,size_t count);
返回值
成功:
包含的头文件
#include
函数原型
ssize_t read(int fd,void*buff,size_t count);
返回值
成功:
复制一个文件到另一个文件
#include
#include
#include
#include
#include int main(int argc,char **argv)
{char buf[512];int read_size=0;int fd1,fd2;fd1 = open(argv[1],O_RDONLY);fd2 = open(argv[2],O_WRONLY|O_CREAT,0666);if(argc != 3){printf("param error!!!");return -1;}if(fd1<0 ||fd2<0){printf("open error!!!");return -1;}while(1){read_size = read(fd1,buf,512);if(read_size<0)break;write(fd2,buf,read_size);}close(fd1);close(fd2);return 0;
}
main函数规定有两个参数
int main(int argc,char** argv)
需要的的头文件
#include
函数原型
off_t lseek(int fd,off_t offset,int whence);
返回值
成功:文件偏移位置值
失败:-1
头文件
#include
函数原型
void sync(void);
Linux具有页缓存机制,在用户与磁盘之间会有一个页缓存区,当写数据后并不会立即写入磁盘,需要等待页缓存区满了以后才会一起送入磁盘,使用sync函数可以将页缓存区的数据立即写入磁盘。
C标准库实现了一个IO缓冲区
常见标准IO函数
文件IO5大模式