文件I/O的操作实例
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的操作实例的更多相关文章
- Java最全文件操作实例汇总
本文实例汇总了Java文件操作.分享给大家供大家参考,具体如下: 1.创建文件夹 ? 1 2 3 4 5 6 7 8 9 10 11 //import java.io.*; File myFolder ...
- Python 文件读写操作实例详解
Python提供了必要的函数和方法进行默认情况下的文件基本操作.你可以用file对象做大部分的文件操作 一.python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法.1.得到当前 ...
- JAVA简单的文件I/O操作实例
如果只是对文件进行普通的读写,可以不用文件流. 以下是实例: File file = new File("test1.txt"); //向文件写入数据的 PrintWriter p ...
- JavaWeb实现文件上传下载功能实例解析
转:http://www.cnblogs.com/xdp-gacl/p/4200090.html JavaWeb实现文件上传下载功能实例解析 在Web应用系统开发中,文件上传和下载功能是非常常用的功能 ...
- 安卓 SQLite数据库操作实例
前段时间写了个安卓平台下SQLite数据库操作的实例 ,一直没得时间总结 ,今天把它弄出来了. 在Android 运行时环境包含了完整的 SQLite. 首先介绍一下SQLite这个数据库: SQLi ...
- 在安卓开发中使用SQLite数据库操作实例
前段时间写了个安卓平台下SQLite数据库操作的实例 ,一直没得时间总结 ,今天把它弄出来了. 在Android 运行时环境包含了完整的 SQLite. 首先介绍一下SQLite这个数据库: SQLi ...
- CentOS 配置防火墙操作实例(启、停、开、闭端口)CentOS Linux-FTP/对外开放端口(接口)TomCat相关
链接地址:http://blog.csdn.net/jemlee2002/article/details/7042991 CentOS 配置防火墙操作实例(启.停.开.闭端口): 注:防火墙的基本操作 ...
- FTP命令具体解释(含操作实例)
以下是微软命令行FTPclient命令大全.假设你想使用"未加工(RAW)"FTP命令而非以下翻译过的请參考:http://www.nsftools.com/tips/RawFTP ...
- CentOS配置防火墙操作实例
CentOS 配置防火墙操作实例(启.停.开.闭端口): 注:防火墙的基本操作命令: 查询防火墙状态: [root@localhost ~]# service iptables status<回 ...
随机推荐
- linux fdisk
fdisk 对硬盘及分区的操作,进入fdisk 对硬盘操作阶段 我们可以对硬盘进行分区操作,前提是您把fdisk -l 弄明白了:通过fdisk -l ,我们能找出机器中所有硬盘个数及设备名称:比如上 ...
- arTemplate解析语法
模板解析语法 defaults.parser = function (code, options) { // var match = code.match(/([\w\$]*)(\b.*)/); // ...
- AC_CONFIG_HEADER
configure.in里有宏AC_CONFIG_HEADER()时用. AC_CONFIG_HEADER宏用于生成config.h文件,以便autoheader使用.
- nefu 753 n!末尾有多少个0
Problem : 753 Time Limit : 1000ms Memory Limit : 65536K description 计算N!末尾有多少个0 input 输入数据有多组,每组1行,每 ...
- office2003
key:
- ecshop 去版权(前台)
该偏文章模板堂搜集总结,包括ecshop前台版权,ecshop后台版权,一个都不留,干干净净,推荐收藏 一.去掉网页标题 Powered by ECShop 打开includes/lib_main.p ...
- CentOS 安装及配置Salt api
安装 # yum -y install salt-api 更改配置 vim /etc/salt/master 把默认的注释掉 我们这边默认配置文件目录是/srv/salt default_inclu ...
- 如果更新包更新包现场,class文件更新过去,没有改变,及时删掉,照样能进那个模块的问题。
这是打更新包需要注意的问题: 带$的同名文件也需要copy过来打更新包,不能只更新一个class文件,找了1天的错误,简直日乐购.
- 巧妙使用Contains()方法查找一个数是否在某堆数中
问题:要判断用户输入的一个数,或者是程序里方法的一个参数的值,或者是一个变量的值是否在某堆数中. 简洁写法:把这堆数放在list中,使用list的Contains()方法检查list是否包含这个数,取 ...
- 转 Problem: AnyConnect was not able to establish a connection to the specified secu
不多说直接上问题,在点击连接时,也是能正常连接上输入密码,在输出用户名密码后 连接后,会出现: problem:anyconnect was net able to establish a conne ...