一. 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的更多相关文章

  1. Mapreduce的文件和hbase共同输入

    Mapreduce的文件和hbase共同输入 package duogemap;   import java.io.IOException;   import org.apache.hadoop.co ...

  2. mapreduce多文件输出的两方法

    mapreduce多文件输出的两方法   package duogemap;   import java.io.IOException;   import org.apache.hadoop.conf ...

  3. 01.SQLServer性能优化之----强大的文件组----分盘存储

    汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql 文章内容皆自己的理解,如有不足之处欢迎指正~谢谢 前天有学弟问逆天:“逆天,有没有一种方 ...

  4. SQL Server 大数据搬迁之文件组备份还原实战

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 解决方案(Solution) 搬迁步骤(Procedure) 搬迁脚本(SQL Codes) ...

  5. SQLSERVER将一个文件组的数据移动到另一个文件组

    SQLSERVER将一个文件组的数据移动到另一个文件组 有经验的大侠可以直接忽视这篇文章~ 这个问题有经验的人都知道怎麽做,因为我们公司的数据量不大没有这个需求,也不知道怎麽做实验 今天求助了QQ群里 ...

  6. SQL Server中的高可用性(2)----文件与文件组

        在谈到SQL Server的高可用性之前,我们首先要谈一谈单实例的高可用性.在单实例的高可用性中,不可忽略的就是文件和文件组的高可用性.SQL Server允许在某些文件损坏或离线的情况下,允 ...

  7. C# ini文件操作【源码下载】

    介绍C#如何对ini文件进行读写操作,C#可以通过调用[kernel32.dll]文件中的 WritePrivateProfileString()和GetPrivateProfileString()函 ...

  8. 【小程序分享篇 一 】开发了个JAVA小程序, 用于清除内存卡或者U盘里的垃圾文件非常有用

    有一种场景, 手机内存卡空间被用光了,但又不知道哪个文件占用了太大,一个个文件夹去找又太麻烦,所以我开发了个小程序把手机所有文件(包括路径下所有层次子文件夹下的文件)进行一个排序,这样你就可以找出哪个 ...

  9. 【原】Android热更新开源项目Tinker源码解析系列之二:资源文件热更新

    上一篇文章介绍了Dex文件的热更新流程,本文将会分析Tinker中对资源文件的热更新流程. 同Dex,资源文件的热更新同样包括三个部分:资源补丁生成,资源补丁合成及资源补丁加载. 本系列将从以下三个方 ...

  10. GreenDao 数据库:使用Raw文件夹下的数据库文件以及数据库升级

    一.使用Raw文件夹下的数据库文件 在使用GreenDao框架时,数据库和数据表都是根据生成的框架代码来自动创建的,从生成的DaoMaster中的OpenHelper类可以看出: public sta ...

随机推荐

  1. 用Hexo搭建属于自己的iOS技术博客,搬家了

    搬家了,本来还打算在博客园混一段时间的,可是当我看到Hexo的时候,已经难以抵挡它的诱惑,简单不简约的界面让我花了整整一天的时间,买域名的过程中发生一点小问题导致DNS解析错误了,但还是成功了.欢迎朋 ...

  2. 5 Best Gantt Chart JIRA Plugins

    Andrew Stepanov/June 23, 2017/6 minutes Software developers enjoy using JIRA software for their proj ...

  3. 第七章Openwrt安装服务器环境php+uhttpd+mysql

    在前面的文章中刷openwrt.配置网络环境.挂载u盘都配置成功了之后,下面的操作就变得简单起来!!!! 1. putty连接到路由器 2. 安装php opkg install php5-fastc ...

  4. 流畅的python第十七章使用期物处理并发

    从 Python 3.4 起,标准库中有两个名为 Future 的类:concurrent.futures.Future 和asyncio.Future.这两个类的作用相同:两个 Future 类的实 ...

  5. Windows安装和配置Tomcat

    1 从http://tomcat.apache.org下载Tomcat压缩包,我这里下的版本是7.0.67.   2 将Tomcat压缩包解压缩到任意路径下,我这里的解压缩路径为E:\tomcat-7 ...

  6. 【License】一张图该诉你各种License的含义?

    一张图该诉你各种License的含义:

  7. PHP数组问题

    转换为数组 对于任意 integer , float , string , boolean 和 resource 类型,如果将一个值转换为数组,将得到一个仅有一个元素的数组,其下标为 0,该元素即为此 ...

  8. Spark1.0.0 history server 配置

    在执行Spark应用程序的时候,driver会提供一个webUI给出应用程序的执行信息.可是该webUI随着应用程序的完毕而关闭port,也就是说,Spark应用程序执行完后,将无法查看应用程序的历史 ...

  9. java运行环境(JRE)

    Java Runtime Environment(JRE) :运行Java程序所必须的环境的集合. JRE的组成: –包括Java 虚拟机 (JVM).Java核心类和支持文件. –不包含开发工具 - ...

  10. python __set__ __get__ __delete__

    class Attr(object): def __init__(self,attrname,attrtype): self.attrname=attrname self.attrtype=attrt ...