系统调用方式文件编程要点_第1页
系统调用方式文件编程要点_第2页
系统调用方式文件编程要点_第3页
系统调用方式文件编程要点_第4页
系统调用方式文件编程要点_第5页
已阅读5页,还剩13页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Linux 文件编程函数一 简述几个基本知识1 Linux 应用程序编程所用到的 函数 ,主要有两种方式提供:系统调用方式 函数库方式2 如何学习这些函数?三步学习法:第一步: 借助工具书,查找函数名; Unix 环境高级编程 第二步:在Linux系统中,利用man命令查看函数信息,并 填写 函数学习手册 。第三步: 实践, 编写代码 。3 VI 概念 文件描述符性质: 一个数字特别含义: 其功能类似于身份证号码, 通过身份证号码, 可以 将对应的公民; 在 Linux 系统中, 每一个 打开的 文件, 都对应 一个数字, 通过这个唯一的数字, 可以找到这个打开的文件, 并 对其进行操作,比如

2、读、写等。二 首先学习 系统调用方式提供的函数 4 学习以下 7 个函数打开文件 创建文件 关闭文件 读文件 写文件 文件定位 复制文件描述符5 打开文件 open范例 1:打开已经存的文件 open.c#include #include #include void main()int fd;/* 文件描述符 */fd = open(/home/test.c,O_RDWR);if(fd0)printf(Open file fali!n);elseprintf(Open file sucessfully!n);范例 2:利用 open 函数创建新文件 open_creat.c#include #

3、include #include void main()int fd;/* 文件描述符 */fd = open(/home/test1.c,O_RDWR | O_CREAT,0755);if(fd0) printf(Open file fali!n);elseprintf(Open file sucessfully!n);6 创建文件 creat 在一书中,找到函数名利用 man creat 查找函数的帮助文件 编写程序代码#include #include #include #include void main()int fd;fd = creat(/home/test2.c,0621);

4、if(fd0)printf(create file fail!n);elseprintf(Create file successfully!n);7 在一书中,找到函数名man close 查找函数原型编写程序代码 close.c#include #include #include #include void main()int fd;fd = open(/home/test2.c,O_RDWR | O_CREAT,0755); if(fd0)printf(create file fail!n);elseprintf(Create file successfully!n);int ret;re

5、t = close(fd);if(ret = 0)printf(File has been closed!n); elseprintf(Fail to close!n);8 首先查找一下读文件的函数 read()man 所查询的是一个手册,首先从章节一里面找关键字,如 果没有找到,再到后续章节中找。man 2 readman 章节一 命令章节二 系统调用章节三 库函数范例:#include void main()int fd; fd = open(/home/test1.c,O_RDWR | O_CREAT,0755); if(fd0)printf(create file fail!n);el

6、se printf(Create file successfully!n);char buf10;/* 定义一个数组,有 10 个空间;用来存放 读取出的数据 */ssize_t count;count = read(fd,buf,5);/*将读取出的字符存放到 buf指向的空间里面。 */if(count=-1) printf(Read fail!);elseprintf(Read %d Bytes.n,count);buf5=0; /* 这样,读取后的内容无乱码字符出现。 */printf(buf is %s.n,buf); /* 打印字符串 */ int ret; ret = close

7、(fd);if(ret = 0)printf(File has been closed!n); elseprintf(Fail to close!n);9 写文件找到写文件对应的函数 write() man 2 writewrite.c#include #include #include #include void main()int fd;fd = open(/home/test2.c,O_RDWR | O_CREAT,0755); if(fd0)printf(create file fail!n);elseprintf(Create file successfully!n);char *b

8、uf = 987654321;ssize_t count;count = write(fd,buf,7); if(count=-1)printf(Write fail!);elseprintf(Write %d Bytes.n,count); printf(The original buf is %s.n,buf);int ret;ret = close(fd);if(ret = 0)printf(File has been closed!n);elseprintf(Fail to close!n);10 定位文件lseek()在此之前,先实验一下,能否在文件写入之后,直接读取刚 才写入的内容

9、?实验验证,没有读到数据。这就是我们要给大家介绍的文件读写指针。当打开文件时,指针位于文件头部,然而,当对文件进行读 或者写操作后,文件指针将发生变化。介绍,如何设置指针的位置。范例:#include #include #include #include #include void main()int fd;fd = open(/home/test2.c,O_RDWR | O_CREAT,0755); if(fd0)printf(create file fail!n);elseprintf(Create file successfully!n); char *buf = 987654321;s

10、size_t count;count = write(fd,buf,7);if(count=-1) printf(Write fail!);elseprintf(Write %d Bytes.n,count); printf(The original buf is %s.n,buf);off_t ref;ref = lseek(fd,0,SEEK_SET);if(ref=-1) printf(Fail to shift.n);elseprintf(The distance to file_head is %d Bytes.n,ref);char buf_r10;/* 定义一个数组,有 10 个

11、空间;用来存放读取出的数据 */ssize_t count_r;count_r = read(fd,buf_r,5);/*将读取出的字符存放到buf 指向的空间里面。 */if(count_r=-1)printf(Read fail!);elseprintf(Read %d Bytes.n,count_r);buf_r5=0; /* 这样,读取后的内容无乱码出现。 */ printf(buf_r is %s.n,buf_r); /* 打印读出的字符串 */int ret;ret = close(fd);if(ret = 0)printf(File has been closed!n); els

12、eprintf(Fail to close!n);11 复制文件描述符 dup()可以利用新复制的文件描述符 fd_new 来操作 oldfd 所 指定的文件。就好比一个人有两个身份证号码。/* 利用 dup() 函数复制后的文件描述符不同于原来的, 但共同指向同一个文件 */范例: dup.c#include #include #include #include #include void main()int fd;fd = open(/home/test2.c,O_RDWR | O_CREAT,0755); if(fd0)printf(create file fail!n);elsepri

13、ntf(Create file successfully!n);char *buf = 987654321; ssize_t count;count = write(fd,buf,7);if(count=-1)printf(Write fail!);elseprintf(Write %d Bytes.n,count);printf(The original buf is %s.n,buf);off_t ref;ref = lseek(fd,0,SEEK_SET); if(ref=-1) printf(Fail to shift.n);elseprintf(The distance to fil

14、e_head is %d Bytes.n,ref);int newfd;newfd = dup(fd);if(newfd=-1)printf(Fail to copy the old fd.n); elseprintf(The new fd is %d.n,newfd);char buf_r10;/* 定义一个数组,有 10 个空间;用来存 放读取出的数据 */ssize_t count_r;count_r = read(newfd,buf_r,5);/* 将读取出的字符存放 到 buf 指向的空间里面。 */if(count_r=-1) printf(Read fail!);elseprin

15、tf(Read %d Bytes.n,count_r);buf_r5=0; /* 这样,读取后的内容无乱码出现。 */ printf(buf_r is %s.n,buf_r); /*打印读出的字符串*/int ret;ret = close(fd);if(ret = 0)printf(File has been closed!n);%dprintf(The oldfd and the newfd are& %d.n,fd,newfd);elseprintf(Fail to close!n);13 综合实例编写文件复制程序cp 参数 1 参数 2源文件 、目标文件 将源文件的所有内容复制到目标文

16、件中; 思路:打开源文件,打开目标文件; 读取源文件的数据,将数据写入目标文件; open read write 查阅编写的函数手册。源文件是什么、目标文件是什么?通过 main() 函数的参数 传进去。编写文件复制程序 copy.c#include #include #include #include #include void main(int argc,char *argv)/*argv0 存放命令的名 字, argv1 存放参数 1, argv2 存放参数 2*/* 打开源文件 */int fd_s;fd_s = open(argv1,O_RDONLY);/* 打开目标文件 */int fd_d;fd_d = open(argv2,O_RDWR | O_CREAT,0666);/* 读取源文件数据 */* 考虑当读取的数据量很大 */char buf512;int count=0;/* 实际读取的数据量 */ while(count = read(fd_s,buf,512)0) /* 将读取出的源文件数据写入目标文件 */ write(fd_d,buf,count);/* 关闭 */ close(fd_s); close(fd_d);运行时,是如何执行的?*/答:./copy /home/test2.c /home/test3.c*中间的数字是什么意思呢?

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论