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 ...
随机推荐
- ORACLE常用性能监控SQL【一】
目录(?)[+] 系列 ORACLE常用性能监控SQL[一] ORACLE常用性能监控SQL[二] Oracle-动态性能视图解读 系列 死锁后的解决办法 生成Kill Session语句 查看导致死 ...
- python笔记10-多线程之线程同步(锁lock)
前言 关于吃火锅的场景,小伙伴并不陌生,吃火锅的时候a同学往锅里下鱼丸,b同学同时去吃掉鱼丸,有可能会导致吃到生的鱼丸. 为了避免这种情况,在下鱼丸的过程中,先锁定操作,让吃火锅的小伙伴停一会,等鱼丸 ...
- pca主成份分析方法
1.应用pca的前提 应用pca的前提是,连续信号具有相关性.相关性是什么,是冗余.就是要利用pca去除冗余. 2.pca的定义 pca是一种去除随机变量间相关性的线性变换.是一种常用的多元数据分析方 ...
- Android内存优化12 内存泄漏常见情况3 注册泄漏
android 中有很多注册和反注册,由于在注册后,上下文自身会被持久化的观察者列表所持有,如果不进行反注册,就会造成内存泄漏 内存泄漏1:Sensor Manager 代码如下: MainActiv ...
- http://www.cnblogs.com/zhengyun_ustc/p/55solution2.html
http://www.cnblogs.com/zhengyun_ustc/p/55solution2.html http://wenku.baidu.com/link?url=P756ZrmasJTK ...
- http://blog.csdn.net/a9529lty/article/details/6454156
http://blog.csdn.net/a9529lty/article/details/6454156
- java实现 tf-idf
1.前言 TF-IDF(term frequency–inverse document frequency)是一种用于信息检索与数据挖掘的常用加权技术.TF意思是词频(Term Frequency), ...
- Oracle数据迁移至HBase操作记录
Oracle数据迁移至HBase操作记录 @(HBase) 近期需要把Oracle数据库中的十几张表T级别的数据迁移至HBase中,过程中遇到了许多苦难和疑惑,在此记录一下希望能帮到一些有同样需求的兄 ...
- php 基于cookie的sessIon机制
session_start()是session机制的开始,它有一定概率开启垃圾回收,因为session是存放在文件中,PHP自身的垃圾回收是无效的,SESSION的回收是要删文件的,这个概率是根据ph ...
- 帮助快速生成页面固定显示元素的jQuery插件 - sticky-kit
来源:GBin1.com 如果需要在用户滚动页面的时候,保持特定元素始终可见的话,今天这里我们介绍的Sticky-Kit是一个不错的选择. 它是一个开源的jQuery插件,可以帮助大家快速针对页面元素 ...