Linux系统lseek函数作用
来源:互联网 发布:软件行业 技术规范 编辑:程序博客网 时间:2024/05/01 20:26
首先看下函数: off_t lseek(int fd, off_t offset, int whence);
所需要头文件:
#include <sys/types.h>
#include <unistd.h>
参数:
fd 表示要操作的文件描述符
offset是相对于whence(基准)的偏移量
whence 可以是SEEK_SET(文件指针开始),SEEK_CUR(文件指针当前位置) ,SEEK_END为文件指针尾
返回值:文件读写指针距文件开头的字节大小,出错,返回-1
lseek 主要作用是移动文件读写指针,因此还有以下两个作用
1.拓展文件,不过一定要一次写的操作。迅雷等下载工具在下载文件时候先扩展一个空间,然后再下载的。
2.获取文件大小
#include<stdio.h>#include<stdlib.h>#include<sys/types.h>#include<unistd.h>#include <sys/stat.h>#include <fcntl.h>void main(){ int add_len = 1024*8; int fd=open("test.txt",O_RDWR); if(fd == -1) { perror("open test.txt"); exit(-1); } lseek(fd,add_len-1,SEEK_END); write(fd,"0",1);}
执行程序后
获取文件长度:
#include<stdio.h>#include<stdlib.h>#include<sys/types.h>#include<unistd.h>#include <sys/stat.h>#include <fcntl.h>void main(){ int fd=open("test.txt",O_RDWR); if(fd == -1) { perror("open test.txt"); exit(-1); } printf("file len:%d \n",lseek(fd,0,SEEK_END)); close(fd);}
1 0
- Linux系统lseek函数作用
- linux 系统函数记录 lseek
- Linux系统库函数 -- lseek函数用法
- Linux系统函数学习之lseek
- Linux系统库函数 -- lseek函数用法
- Linux系统库函数 -- lseek函数用法
- Linux系统库函数 -- lseek函数用法
- Linux系统库函数 -- lseek函数用法
- Linux系统库函数 -- lseek函数用法
- Linux ---lseek() 函数
- linux文件函数-lseek
- Linux ---lseek() 函数
- Linux ---lseek() 函数
- Linux---lseek函数
- 系统函数-文件操作-lseek()
- Linux系统编程--lseek调用
- linux下 lseek函数用法
- linux下lseek()函数理解
- 剖析Disruptor:为什么会这么快?(一)Ringbuffer的特别之处
- 循环赛事、对称矩阵
- 整理下混乱的生活
- leetcode--Kth Smallest Element in a BST
- H.264视频数据组织结构-GOP图像组和组中3类不同的图像帧
- Linux系统lseek函数作用
- 出自国外某牛人的一道面试题(About 42)
- 【Leetcode Algorithm】Remove Element
- c++ stl资源--qsort排序
- 【Leetcode Algorithm】Implement strStr()
- POJ 1163 The Triangle
- java学习07--运算符:三元运算符
- qtbase\\configure.exe' 不是内部或外部命令,也不是可运行的程序或批处理文件
- 求10000以内质数(以前都是直接打表,现在问到怎么求,瞬间词穷了,还是应该搞懂)