seek接口
来源:互联网 发布:淘宝店铺号码怎么查找 编辑:程序博客网 时间:2024/04/29 11:59
seek接口用来对设备的读写位置进行定位。file_operations结构中对应的seek接口如下:
- loff_t (*llseek) (struct file *filp, loff_t off, int whence)
其中off是偏移量,whence参数指起点位置。
例1.6 字符设备seek实例
代码见光盘\src\1drivermodel\1-6lseek。核心代码如下所示:
应用程序参考代码如下:
- ssize_t simple_read(struct file *filp, char __user *buf, size_t count,loff_t *f_pos)
- {
- loff_t pos= *f_pos;//获取文件指针
- if(pos>=256)
- {
- count=0;
- goto out;
- }
- if(count>(256-pos))
- {
- count=256-pos;
- }
- pos += count;
- //复制数据到指定的地址
- if (copy_to_user(buf,demoBuffer+*f_pos,count))
- {
- count=-EFAULT;
- goto out;
- }
- *f_pos = pos;
- out:
- return count;
- }
- loff_t simple_llseek(struct file *filp, loff_t off, int whence)
- {
- loff_t pos;
- pos = filp->f_pos;
- switch (whence)
- {
- case 0:
- pos = off;
- break;
- case 1:
- pos += off;
- break;
- case 2:
- pos =255+off;
- break;
- default:
- return -EINVAL;
- }
- if ((pos>=256) || (pos<0))
- {
- return -EINVAL;
- }
- return filp->f_pos=pos;
- }
- struct file_operations simple_fops = {
- .owner = THIS_MODULE,
- .llseek = simple_llseek,
- .read = simple_read,
- .open = simple_open,
- .release = simple_release,
- };
本例运行结果如下:
- void main()
- {
- int fd;
- int i;
- char data[256];
- int retval;
- fd=open("/dev/fgj",O_RDWR);
- if(fd==-1)
- {
- perror("error open\n");
- exit(-1);
- }
- printf("open /dev/fgj successfully\n");
- retval=lseek(fd,5,0);
- if(retval==-1)
- {
- perror("lseek error\n");
- exit(-1);
- }
- retval=read(fd,data,3);
- if(retval==-1)
- {
- perror("read error\n");
- exit(-1);
- }
- data[retval]=0;
- printf("read successfully:%s\n",data);
- //文件定位
- retval=lseek(fd,2,0);
- if(retval==-1)
- {
- perror("lseek error\n");
- exit(-1);
- }
- retval=read(fd,data,3);
- if(retval==-1)
- {
- perror("read error\n");
- exit(-1);
- }
- data[retval]=0;
- printf("read successfully:%s\n",data);
- close(fd);
- }
- [root@urbetter /home]# insmod demo.ko
- [root@urbetter /home]# mknod /dev/fgj c 224 0
- [root@urbetter /home]# ./test
- open /dev/fgj successfully
- read successfully:FGH
- read successfully:CDE
0 0
- seek接口
- seek
- seek
- seek()
- RandomAccessFile.seek
- Ffmpeg seek
- seek function
- python seek()
- python seek()
- mp4 seek
- python seek
- U - Seek the Name, Seek the Fame
- Seek the Name, Seek the Fame(KMP)
- POJ2752 Seek the Name, Seek the Fame
- POJ2752:Seek the Name, Seek the Fame
- poj2752 Seek the Name, Seek the Fame
- Seek the Name, Seek the Fame
- Seek the Name, Seek the Fame(kmp)
- CString对象的一种错误的使用方式
- ioctl接口
- js学习小结(三)2014.4.18-外边距合并,负边距和浏览器渲染,盒模型
- Java中内存中的Heap(堆)、Stack(栈)与程序运行的关系
- My97DatePicker日历实现开始日期小于结束日期验证
- seek接口
- poj 2565 Ants (KM+思维)
- Java知识总结——LinkedList和ArrayList的区别
- OPENCV中滑动条的使用
- The Right Way to Swizzle in Objective-C
- sql2005的northwind
- select、pselect、poll和epoll的区别
- 为何会产生warning: no newline at end of file?
- poll接口