一. open()&close()

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h> int main()
{ int fd;
fd = open("abc.txt", O_CREAT|O_RDWR, ); // 若flag中使用了O_CREAT,则需要指定第三个参数访问权限
if (fd < )
printf("file create failure");
printf("current fd is: %d\n", fd);
close(fd);
return ;
}

二.read()&write()

write.c

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h> static const char *str = "http://www.baidu.com\n"; int main()
{
int fd;
fd = open("cde.txt", O_CREAT|O_RDWR|O_APPEND, ); write(fd, str, strlen(str));
close(fd);
return ;
}

read.c

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h> int main()
{ char tmp[];
char str[]; int wr_fd;
wr_fd = open("aaa.txt", O_CREAT|O_WRONLY|O_APPEND, ); int rd_fd;
rd_fd = open("cde.txt", O_RDONLY); int total = , len;
while((len = read(rd_fd, tmp, ))) {
strncpy(str+total, tmp, len);
total+=len;
}
str[total-] = '\0'; close(wr_fd);
close(rd_fd);
printf("str is: %s\n", str);
return ;
}

运行结果:

str is: http://www.baidu.com
http://www.taobao.com
http://www.qq.com
http://www.dota2.com.cn
http://www.tmall.com
http://www.jd.com
http://www.apple.com
http://www.microsoft.com

三.lseek() 移动文件读写指针

使用lseek插入文件内容

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h> static const char *str = "http://www.ibm.com\n"; int main()
{ int fd;
off_t offset;
fd = open("cde.txt", O_RDWR);
offset = lseek(fd, , SEEK_END); write(fd, str, strlen(str));
close(fd);
printf("cur offset is: %d\n", offset);
return ;
}

运行结果:

http://www.qq.com
http://www.dota2.com.cn
http://www.tmall.com
http://www.jd.com
http://www.apple.com
http://www.microsoft.com
http://www.ibm.com
http://www.ibm.com

使用lseek计算文件大小

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h> int main()
{ int fd;
off_t offset; fd = open("cde.txt", O_RDWR); offset = lseek(fd, , SEEK_END);
printf("cur file size is: %d\n", offset);
close(fd);
return ;
}

运行结果:

cur file size is: 208

-rwxrwxr-x 1 yongdaimi yongdaimi 208 Jan 29 00:54 cde.txt

四.fcntl()

使用fcntl()获得文件的flag标志

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h> int main()
{ int fd;
if ((fd = open("hh.txt", O_RDWR | O_CREAT | O_EXCL, )) == -) {
perror("open error");
exit();
} int var;
if ((var = fcntl(fd, F_GETFL, )) < ) {
perror("fcntl error");
close(fd);
exit();
} switch(var & O_ACCMODE) {
case O_RDONLY:
printf("Read only ...\n");
break;
case O_WRONLY:
printf("Write only ...\n");
break;
case O_RDWR:
printf("Read And Write ...\n");
break;
default:
printf("Do't know...\n");
break;
} if (var & O_APPEND) {
printf("And Append...\n");
}
if (var & O_NONBLOCK) {
printf("And Blocking...\n");
}
if (close(fd) == -) {
perror("close error");
} return ;
}

运行结果:

Read And Write ...

五.ioctl()

使用TIOCGWINSZ命令获得终端设备的窗口大小

#include <sys/ioctl.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h> int main()
{ 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 ;
}

运行结果:

36 rows, 121 columns

最新文章

  1. 旧版本APP被开发人员下架,新版本重新上传依然显示被下架
  2. 「C++11」Lambda 表达式
  3. 了解vmware tools
  4. sql 多行转换为一行
  5. Postman-CI集成Jenkins
  6. WebService SendTimeout 超时问题
  7. struts2-json-plugin插件实现异步通信
  8. 国内外php主流开源cms、SNS、DIGG、RSS、Wiki汇总
  9. 理解MVVM模式
  10. Swing Dance!摇摆舞!小组
  11. svn强制用户提交时写日志
  12. Flask -- 路由
  13. 网关、DNS、路由器区别
  14. 单向链表的Java实现
  15. python3,打印一年的某一天是一年的第几天
  16. 关于Java流
  17. [转]POI实现读写Excel2007完整示例
  18. yii2常用的migrate命令
  19. [LOJ6029~6052]雅礼集训 2017 选做
  20. Entity Framework 数据生成选项DatabaseGenerated(转)

热门文章

  1. 【shiro】使用shiro搭建的项目,页面引用js,报错:Uncaught SyntaxError: Unexpected token &lt;
  2. 2014 linux
  3. Unity-EasyTouch插件之Two Finger
  4. python tkinter 框架开发的收费音乐免费下载工具
  5. ylbtech-LanguageSamples-Indexers(索引器)
  6. iOS:quartz2D绘图(处理图像,绘制图像并添加水印)
  7. 【FireMonkey】StyleBook使用方法
  8. PL/SQL如何远程连接ORACLE
  9. TensorFlow------读取CSV文件实例
  10. 不输入sudo使用docker