lseek,fcntl,ioctl函数
函数说明:
每一个已打开的文件都有一个读写位置, 当打开文件时通常其读写位置是指向文件开头, 若是以附加的方式打开文件(如O_APPEND), 则读写位置会指向文件尾. 当read()或write()时, 读写位置会随之增加,lseek()便是用来控制该文件的读写位置. 参数fildes 为已打开的文件描述词, 参数offset 为根据参数whence来移动读写位置的位移数.
注意其每个打开的文件都记录着当前读写位置,打开文件时读写位置是0,表示文件开头,通常读写多少个字节就会将读写位置往后移多少个字节。但是有一个例外,如果以O_APPEND方式打开,每次写操作都会在文件末尾追加数据,然后将读写位置移到新的文件末尾。lseek和标准I/O库的fseek函数类似,可以移动当前读写位置(或者叫偏移量)。

lseek扩展文件:
touch创建文件test,注意其大小为0:

#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
int main(void)
{
int fd=open("test",O_RDWR);//假设已经存在test文件并且有读写权限
if(fd<)
{
perror("open test");
exit(-);
}
//扩展一个文件,一定要有写操作 0x1000为4096
lseek(fd,0x1000,SEEK_SET);
write(fd,"a",); close(fd);
return ;
}
运行结果:

可以看到文件大小变成了4097,为4096加上我们写入的一个字符a。注意,如果程序中没有write函数进行写操作,test文件大小不会改变。
lseek获取文件大小:
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
int main(void)
{
int fd=open("hello",O_RDWR);
if(fd<)
{
perror("open hello");
exit(-);
}
long long int len;
//第二个参数到SEEK_END的长度,off_t在64为系统中,通常为long long int,32的为long
len= lseek(fd,,SEEK_END);
printf("%lld\n",len); close(fd);
return ;
}
创建hello文件,写入hello文本,一共6个字符,包含最后一个\n.
输出:

通过od指令也可以看出大小为6.

fcntl
先前我们以read终端设备为例介绍了非阻塞I/O,为什么我们不直接对STDIN_FILENO做
非阻塞read,而要重新open一遍/dev/tty呢?因为STDIN_FILENO在程序启动时已经被自动
打开了,而我们需要在调用open时指定O_NONBLOCK标志。这里介绍另外一种办法,可以用
fcntl函数改变一个已打开的文件的属性,可以重新设置读、写、追加、非阻塞等标志(这
些标志称为File Status Flag),而不必重新open文件。


#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/ioctl.h>
int main(void)
{
struct winsize size;
if (isatty(STDOUT_FILENO) == )
{
exit();
}
if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &size)<)
{
perror("ioctl TIOCGWINSZ error");
exit();
}
printf("%d rows, %d columns\n", size.ws_row, size.ws_col);
return ;
}
在图形界面的终端里多次改变终端窗口的大小并运行该程序,观察结果。

没有人是所有内核驱动或者应用函数都记得的,所以很多时候,我们需要借助谷歌必应百度搜索,一点一点积累。
lseek,fcntl,ioctl函数的更多相关文章
- 六、文件IO——fcntl 函数 和 ioctl 函数
6.1 fcntl 函数 6.1.1 函数介绍 #include <unistd.h> #include <fcntl.h> int fcntl(int fd, int cmd ...
- lseek,fseek,fcntl,ioctl
lseek函数 文件偏移 Linux中可使用系统函数lseek来修改文件偏移量(读写位置) 每个打开的文件都记录着当前读写位置,打开文件时读写位置是0,表示文件开头,通常读写多少个字节就会将读写位置往 ...
- 第3章 文件I/O(4)_dup、dup2、fcntl和ioctl函数
5. 其它I/O系统调用 (1)dup和dup2函数 头文件 #include<unistd.h> 函数 int dup(int oldfd); int dup2(int oldfd, i ...
- Samsung_tiny4412(驱动笔记05)----Makefile,open,read,write,lseek,poll,ioctl,fasync
/*********************************************************************************** * * Makefile,op ...
- IOCTL函数用法
http://blog.163.com/he_junwei/blog/static/19793764620152510533753/ http://blog.csdn.net/styyzxjq2009 ...
- Linux下利用ioctl函数获取网卡信息
linux下的ioctl函数原型如下: #include <sys/ioctl.h> int ioctl(int handle, int cmd, [int *argc, int argv ...
- Linux系统编程(4)——文件与IO之ioctl函数
ioctl是设备驱动程序中对设备的I/O通道进行管理的函数.所谓对I/O通道进行管理,就是对设备的一些特性进行控制,例如串口的传输波特率.马达的转速等等.它的参数个数如下:int ioctl(int ...
- ioctl函数详细说明(网络)
ioctl 函数 本函数影响由fd 参数引用的一个打开的文件. #include<unistd.h> int ioctl( int fd, int request, .../* void ...
- (笔记)Linux下的ioctl()函数详解
我这里说的ioctl函数是指驱动程序里的,因为我不知道还有没有别的场合用到了它,所以就规定了我们讨论的范围.写这篇文章是因为我前一阵子被ioctl给搞混了,这几天才弄明白它,于是在这里清理一下头脑. ...
- 获取网络接口信息——ioctl()函数与结构体struct ifreq、 struct ifconf
转载请注明出处:windeal专栏 Linux 下 可以使用ioctl()函数 以及 结构体 struct ifreq 结构体struct ifconf来获取网络接口的各种信息. ioctl 首先看 ...
随机推荐
- 关于TagHelper的那些事情——如何自定义TagHelper(TagHelper基类)
写在开头 前面介绍了TagHelper的基本概念和内嵌的TagHelpers,想必大家对TagHelper都有一定的了解.TagHelper看上去有点像WebControl,但它不同于WebContr ...
- [转]SSIS高级转换任务—行计数
本文转自:http://www.cnblogs.com/tylerdonet/archive/2011/06/19/2084780.html 在SSIS中的Row Count转换可以在数据流中计算数据 ...
- PHP之is_a()函数执行代码之总结
今天看到云舒在群里贴的漏洞公告,原始的文章在 http://www.byte.nl/blog/2011/09/23/security-bug-in-is_a-function-in-php-5-3-7 ...
- http://www.blogjava.net/xylz/archive/2010/07/08/325587.html
http://www.blogjava.net/xylz/archive/2010/07/08/325587.html
- SQL Server 2008数据库备份与恢复
在这所要说的是“分离/附加”数据库: 1.分离数据库就是将某个数据库(如student_Mis)从SQL Server数据库列表中删除,使其不再被SQL Server管理和使用,但该数据库的文件(.M ...
- MySQL建表设置外键提示错误
错误内容: ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to ...
- 在 TDA 工具里看到 Java Thread State 的第一反应是
转载:http://itindex.net/detail/43158-tda-%E5%B7%A5%E5%85%B7-java 使用 TDA 工具,看到大量 Java Thread State 的第 ...
- LVS-负载均衡
LVS: LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统.本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一.章 ...
- UVA270-Lining Up
斜率斜率斜率......... #include<iostream> #include<cstdio> #include<algorithm> #include&l ...
- redis3.0.6安装配置
运行linux客户端 1 安装依赖 yum install gcc-c++ -y(安装gcc) 2 创建安装目录.编译.安装 mkdir -p /usr/local/redis(创建安 ...