1_open.c:

#include <sys/types.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h> int main(int argc,char *argv[])
{
int fd; //文件描述符 /*以读的方式打开一个文件,如果文件不存在,则返回错误*/
fd = open("test.txt",O_RDONLY);
if(fd < ){
perror("open");
return -;
}
printf("fd == %d\n",fd); return ;
}

2_open.c:

#include <sys/types.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h> int main(int argc,char *argv[])
{
int fd; //文件描述符 /*以读的方式打开一个文件,如果文件不存在,则创建它*/
fd = open("test.txt",O_RDONLY | O_CREAT,);
if(fd < ){
perror("open");
return -;
}
printf("fd == %d\n",fd); return ;
}

3_read.c:

#include <sys/types.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h> int main(int argc,char *argv[])
{
int fd; //文件描述符
int ret;
char buf[] = {}; /*读取标准终端 : 0 (STDIN_FILENO)*/
ret = read(,buf,sizeof(buf)-);
if(ret > ){
/*读取成功*/ /*打印到终端*/
printf("%s\n",buf);
} return ;
}

4_read.c:

#include <sys/types.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h> int main(int argc,char *argv[])
{
int fd; //文件描述符
int ret;
char buf[] = {}; /*打开一个文件,以读的方式*/
fd = open("test.txt",O_RDONLY);
if(fd < ){
perror("open test");
return -;
} /*把文件指针移动到文件头*/
lseek(fd,,SEEK_SET); /*读取标准终端 : 0 (STDIN_FILENO)*/
ret = read(fd,buf,sizeof(buf)-);
if(ret > ){
/*读取成功*/ /*打印到终端*/
printf("%s\n",buf);
} return ;
}

5_write.c:

#include <sys/types.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h> int main(int argc,char *argv[])
{
int fd; //文件描述符
int ret;
char buf[] = {}; /*打开一个文件,以读的方式*/
fd = open("test.txt",O_RDONLY);
if(fd < ){
perror("open test");
return -;
} /*把文件指针移动到文件头*/
lseek(fd,,SEEK_SET); /*读取标准终端 : 0 (STDIN_FILENO)*/
ret = read(fd,buf,sizeof(buf)-);
if(ret > ){
/*读取成功*/ /*打印到终端*/
write(,buf,ret);
} return ;
}

6_write_read.c:

#include <sys/types.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h> int main(int argc,char *argv[])
{
int fdr; //文件描述符
int fdw;
int ret;
char buf[] = {}; /*打开一个文件,以读的方式*/
fdr = open("test.txt",O_RDONLY);
if(fdr < ){
perror("open test");
return -;
} /*打开一个文件dest.txt,用于写,如果文件不存在,则创建*/
fdw = open("dest.txt",O_WRONLY | O_CREAT,);
if(fdw < ){
perror("open dest");
return -;
} /*把文件指针移动到文件头*/
lseek(fdr,,SEEK_SET); /*读取标准终端 : 0 (STDIN_FILENO)*/
ret = read(fdr,buf,sizeof(buf)-);
if(ret > ){
/*读取成功*/ /*写入到文件dest.txt*/
write(fdw,buf,ret);
} return ;
}

文件I/O的操作实例的更多相关文章

  1. Java最全文件操作实例汇总

    本文实例汇总了Java文件操作.分享给大家供大家参考,具体如下: 1.创建文件夹 ? 1 2 3 4 5 6 7 8 9 10 11 //import java.io.*; File myFolder ...

  2. Python 文件读写操作实例详解

    Python提供了必要的函数和方法进行默认情况下的文件基本操作.你可以用file对象做大部分的文件操作 一.python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法.1.得到当前 ...

  3. JAVA简单的文件I/O操作实例

    如果只是对文件进行普通的读写,可以不用文件流. 以下是实例: File file = new File("test1.txt"); //向文件写入数据的 PrintWriter p ...

  4. JavaWeb实现文件上传下载功能实例解析

    转:http://www.cnblogs.com/xdp-gacl/p/4200090.html JavaWeb实现文件上传下载功能实例解析 在Web应用系统开发中,文件上传和下载功能是非常常用的功能 ...

  5. 安卓 SQLite数据库操作实例

    前段时间写了个安卓平台下SQLite数据库操作的实例 ,一直没得时间总结 ,今天把它弄出来了. 在Android 运行时环境包含了完整的 SQLite. 首先介绍一下SQLite这个数据库: SQLi ...

  6. 在安卓开发中使用SQLite数据库操作实例

    前段时间写了个安卓平台下SQLite数据库操作的实例 ,一直没得时间总结 ,今天把它弄出来了. 在Android 运行时环境包含了完整的 SQLite. 首先介绍一下SQLite这个数据库: SQLi ...

  7. CentOS 配置防火墙操作实例(启、停、开、闭端口)CentOS Linux-FTP/对外开放端口(接口)TomCat相关

    链接地址:http://blog.csdn.net/jemlee2002/article/details/7042991 CentOS 配置防火墙操作实例(启.停.开.闭端口): 注:防火墙的基本操作 ...

  8. FTP命令具体解释(含操作实例)

    以下是微软命令行FTPclient命令大全.假设你想使用"未加工(RAW)"FTP命令而非以下翻译过的请參考:http://www.nsftools.com/tips/RawFTP ...

  9. CentOS配置防火墙操作实例

    CentOS 配置防火墙操作实例(启.停.开.闭端口): 注:防火墙的基本操作命令: 查询防火墙状态: [root@localhost ~]# service iptables status<回 ...

随机推荐

  1. Android截图命令screencap

    查看帮助命令 bixiaopeng@bixiaopeng ~$ adb shell screencap -v screencap: invalid option -- v usage: screenc ...

  2. <context:annotation-config/>

    转自:Spring <context:annotation-config/> 解说 在基于主机方式配置Spring的配置文件中,你可能会见到<context:annotation-c ...

  3. classname 就是在css上添加类,然后js的类名等于

      <!DOCTYPE HTML>   <html>   <head>   <meta http-equiv="Content-Type" ...

  4. hdu_2089_不要62(数位DP)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意:中文,不解释 题解:dp[i][j]表示当前第i位的前一个数为j,然后记忆化dfs,注意的 ...

  5. shell脚本中$

    变量名只能包含数字.字母和下划线,因为某些包含其他字符的变量有特殊含义,这样的变量被称为特殊变量. 例如,$ 表示当前Shell进程的ID,即pid ./package/kernel/mac80211 ...

  6. IoC容器Autofac正篇之简单实例(四)

    先上一段代码. namespace ConsoleApplication3 { class Program { static void Main(string[] args) { ContainerB ...

  7. 超赞!聊聊WEB APP、HYBRID APP与NATIVE APP的设计差异

    编者按:这3类主流应用你都了解吗?设计师除了要有视觉功夫,对不同形式的APP也应当了然于胸,今天百度的同学写了一篇非常全面的总结,帮你迅速搞定3类主流APP的设计方法,附带一大波避雷针,带你巧妙跳过A ...

  8. ios 烟花 火焰 雨水 雪花等特效属性

    CAEmitterLayer *snowEmitter = [CAEmitterLayer layer]; //例子发射位置 snowEmitter.emitterPosition = CGPoint ...

  9. 用TLS实现安全TCP传输及配置和访问https的web服务(转)

    tls相关 大致原理 为了让两个之间实现安全传输,(我们把服务端统一叫做TcpServer,客户端统一叫做TcpClient),TcpServer在listen完了accept之后要用一个证书来声明自 ...

  10. 百度,google的地理编码

    1.百度的地理编码:(不支持中国以外的其它城市) http://api.map.baidu.com/geocoder/v2/?ak=E974997f80db18330f8f5c61d084a677&a ...