文件IO---lseek、fcntl函数
来源:互联网 发布:哪个cms好 编辑:程序博客网 时间:2024/05/01 14:17
lseek
lseek()用来控制文件的读写位置。(移动读写指针位置)。
每个打开的文件都记录着当前读写位置,打开文件时读写位置是0,表示文件开头,通常读写多少个字节就会将读写位置往后移多少个字节。
用法:
#include <sys/types.h>#include <unistd.h>off_t lseek(int fd, off_t offset, int whence);/*lseek成功返回偏移量,失败返回-1*/
whence:
- SEEK_SET 将读写位置指向文件头后再增加offset个位移量
- SEEK_CUR 以目前的读写位置往后增加offset个位移量
- SEEK_END 将读写位置指向文件尾后再增加offset个位移量
实例:
1)用lseek拓展一个空文件abc。
lseek_test1.c
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <errno.h>int main(void){ int fd; fd = open("abc", O_RDWR); if(fd < 0) { perror("open abc"); exit(-1); } /* 0x1000相当于4096字节 */ lseek(fd, 0x1000, SEEK_SET); /* 扩展一个文件,一定要有一次写操作 */ write(fd, "a", 1); close(fd); return 0;}
2)lseek获取一个文件的大小。
lseek_test2.c
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>int main(void){ int fd; fd = open("hello", O_RDONLY); if(fd < 0) { perror("open hello"); exit(-1); } printf("%d\n", lseek(fd, 0, SEEK_END)); close(fd); return 0;}
gcc lseek_test2.c -o lseek_test2
fcntl
fcntl获取或设置文件访问控制属性。可改变一个已打开的文件的属性,可以重新设置读、写、追加、非阻塞等标志(这 些标志称为File Status Flag),而不必重新open文件。
用法:
#include <unistd.h>#include <fcntl.h>/* 可变参函数 */int fcntl(int fd, int cmd, ... /* arg */ );
可变参数的类型和个数取决于前面的 cmd参数。
File Status Flag:
- F_GETFL 获得文件状态标志
- F_SETFL 设置文件状态标志
实例:
fcntl改变一个已打开文件的属性。
对非阻塞读终端代码修改。
...#define MSG_TRY "try again\n"int main(void){ int flags, n; char buf[10]; /* 这样就不用重新open /dev/tty 设置O_NONBLOCK*/ flags = fcntl(STDIN_FILENO, F_GETFL); flags |= O_NONBLOCK; if(fcntl(STD_FILENO, F_SETFL, flags) == -1) { perror("fcntl"); exit(1); }tryagain:......}
0 0
- 文件IO---lseek、fcntl函数
- Linux文件I/O的lseek,fcntl和ioctl函数
- 8-文件IO-fcntl函数
- 文件IO lseek
- Linux 文件IO函数(open/close/read/write/lseek)
- Linux编程学习之文件IO篇-lseek函数
- 文件IO操作之文件锁fcntl()函数
- linux文件函数-lseek
- 在linux中使用open函数对文件进行操作后如何读取文件中的内容 lseek fcntl ioctl
- open read write fcntl lseek等函数的使用整理:
- open read write fcntl lseek等函数的使用整理:
- 文件/目录管理(2)---dup()/dup2()/lseek()/fcntl()
- 系统函数-文件操作-lseek()
- apue读书笔记【六】:文件IO(2) lseek
- IO lseek
- 七、Linux系统编程-文件和IO(五)fcntl函数及常用操作、文件锁
- Linux下编程------文件与IO(三) 文件共享和fcntl函数
- 文件锁 fcntl()函数说明
- 用PHP做网站的PV、UV、IP访客统计
- UVA 11029--取n的k次幂的前m位数
- 寄存器
- 运用栈求解迷宫问题C/C++
- [leetcode] 400. Nth Digit
- 文件IO---lseek、fcntl函数
- 趣写算法系列之--匈牙利算法
- 判断字符串中的括号是否成对出现
- equals方法与hashcode方法
- uprobe试用小结
- 系统命令注入的介绍与代码防御
- shell脚本调用java jar包 的问题总结
- 数据库索引
- 分享一道有意思的HTML题