1.文件I/O
一. 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.文件I/O的更多相关文章
- Mapreduce的文件和hbase共同输入
Mapreduce的文件和hbase共同输入 package duogemap; import java.io.IOException; import org.apache.hadoop.co ...
- mapreduce多文件输出的两方法
mapreduce多文件输出的两方法 package duogemap; import java.io.IOException; import org.apache.hadoop.conf ...
- 01.SQLServer性能优化之----强大的文件组----分盘存储
汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql 文章内容皆自己的理解,如有不足之处欢迎指正~谢谢 前天有学弟问逆天:“逆天,有没有一种方 ...
- SQL Server 大数据搬迁之文件组备份还原实战
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 解决方案(Solution) 搬迁步骤(Procedure) 搬迁脚本(SQL Codes) ...
- SQLSERVER将一个文件组的数据移动到另一个文件组
SQLSERVER将一个文件组的数据移动到另一个文件组 有经验的大侠可以直接忽视这篇文章~ 这个问题有经验的人都知道怎麽做,因为我们公司的数据量不大没有这个需求,也不知道怎麽做实验 今天求助了QQ群里 ...
- SQL Server中的高可用性(2)----文件与文件组
在谈到SQL Server的高可用性之前,我们首先要谈一谈单实例的高可用性.在单实例的高可用性中,不可忽略的就是文件和文件组的高可用性.SQL Server允许在某些文件损坏或离线的情况下,允 ...
- C# ini文件操作【源码下载】
介绍C#如何对ini文件进行读写操作,C#可以通过调用[kernel32.dll]文件中的 WritePrivateProfileString()和GetPrivateProfileString()函 ...
- 【小程序分享篇 一 】开发了个JAVA小程序, 用于清除内存卡或者U盘里的垃圾文件非常有用
有一种场景, 手机内存卡空间被用光了,但又不知道哪个文件占用了太大,一个个文件夹去找又太麻烦,所以我开发了个小程序把手机所有文件(包括路径下所有层次子文件夹下的文件)进行一个排序,这样你就可以找出哪个 ...
- 【原】Android热更新开源项目Tinker源码解析系列之二:资源文件热更新
上一篇文章介绍了Dex文件的热更新流程,本文将会分析Tinker中对资源文件的热更新流程. 同Dex,资源文件的热更新同样包括三个部分:资源补丁生成,资源补丁合成及资源补丁加载. 本系列将从以下三个方 ...
- GreenDao 数据库:使用Raw文件夹下的数据库文件以及数据库升级
一.使用Raw文件夹下的数据库文件 在使用GreenDao框架时,数据库和数据表都是根据生成的框架代码来自动创建的,从生成的DaoMaster中的OpenHelper类可以看出: public sta ...
随机推荐
- 获取dataset结果集的第一行第一列字段
DataSet fileNameDs = DbHelper.excuteSqlResultDataSet(strSql); ) { DataTable fileNameDt = fileNameDs. ...
- js中几种常见的方法的实例 shift,unshift,push,prop
1.shift()定义和用法 shift() 方法用于把数组的第一个元素从其中删除,并返回第一个元素的值. 语法:arrayObject.shift() 返回值:数组原来的第一个元素的值. 说明:如果 ...
- Hive:用Java代码通过JDBC连接Hiveserver
参考https://www.iteblog.com/archives/846.html 1.hive依赖hadoop,将hdfs当作文件存储介质,那是否意味着hive需要知道namenode的地址? ...
- unity3d由多个部分组成一个角色
摘自http://forum.unity3d.com/threads/16485-quot-stitch-multiple-body-parts-into-one-character-quot So ...
- zabbix3.2 报错 Database error
一.Database errorThe frontend does not match Zabbix database. Current database version (mandatory/opt ...
- 流畅的python第九章符合Python风格的对象学习记录
对象表示形式 每门面向对象的语言至少都有一种获取对象的字符串表示形式的标准方式.Python提供了两种方式 repr()便于开发者理解的方式返回对象的字符串表示形式 str()便于用户理解的方式返回对 ...
- gdc skin
https://www.gdcvault.com/play/1024410/Achieving-High-Quality-Low-Cost 这篇是教美术怎么用做地形那种方法 复用贴图 做skin的 做 ...
- vue项目中使用地图组件
一.引入高德地图 一般用使用vue-cli webpack最简单粗暴的引入地图api的方法就是,在入口index.html的头部直接引入,记得一定要带上key,如果没有的话去高德地图api的官网申请一 ...
- DevExpress 小计 GridControl 隔行换行
摘自: http://www.cnblogs.com/yuerdongni/archive/2012/09/08/2676753.html 1. 如何解决单击记录整行选中的问题 View->Op ...
- LeetCode——3Sum & 3Sum Closest
3Sum 题目 Given an array S of n integers, are there elements a,b,c in S such that a + b + c = 0? Find ...