1.文件I/O
2024-09-04 11:30:43
一. 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
最新文章
- 旧版本APP被开发人员下架,新版本重新上传依然显示被下架
- 「C++11」Lambda 表达式
- 了解vmware tools
- sql 多行转换为一行
- Postman-CI集成Jenkins
- WebService SendTimeout 超时问题
- struts2-json-plugin插件实现异步通信
- 国内外php主流开源cms、SNS、DIGG、RSS、Wiki汇总
- 理解MVVM模式
- Swing Dance!摇摆舞!小组
- svn强制用户提交时写日志
- Flask -- 路由
- 网关、DNS、路由器区别
- 单向链表的Java实现
- python3,打印一年的某一天是一年的第几天
- 关于Java流
- [转]POI实现读写Excel2007完整示例
- yii2常用的migrate命令
- [LOJ6029~6052]雅礼集训 2017 选做
- Entity Framework 数据生成选项DatabaseGenerated(转)
热门文章
- 【shiro】使用shiro搭建的项目,页面引用js,报错:Uncaught SyntaxError: Unexpected token <;
- 2014 linux
- Unity-EasyTouch插件之Two Finger
- python tkinter 框架开发的收费音乐免费下载工具
- ylbtech-LanguageSamples-Indexers(索引器)
- iOS:quartz2D绘图(处理图像,绘制图像并添加水印)
- 【FireMonkey】StyleBook使用方法
- PL/SQL如何远程连接ORACLE
- TensorFlow------读取CSV文件实例
- 不输入sudo使用docker