版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Linux文件编程函数一 简述几个基本知识1 Linux应用程序编程所用到的函数,主要有两种方式提供: 系统调用方式 函数库方式2 如何学习这些函数?三步学习法:第一步:借助工具书,查找函数名;Unix环境高级编程第二步:在Linux系统中,利用man命令查看函数信息,并填写函数学习手册。第三步:实践,编写代码。3 VI概念文件描述符 性质: 一个数字 特别含义:其功能类似于身份证号码,通过身份证号码,可以将对应的公民;在Linux系统中,每一个 打开的 文件,都对应一个数字,通过这个唯一的数字,可以找到这个打开的文件,并对其进行操作,比如读、写等。二 首先学习系统调用方式提供的函数4 学习以
2、下7个函数打开文件 创建文件 关闭文件 读文件 写文件 文件定位 复制文件描述符5 打开文件open范例1:打开已经存的文件 open.c#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>void main()int fd;/*文件描述符*/fd = open("/home/test.c",O_RDWR);if(fd<0) printf("Open file fali!n");else printf("Open file suces
3、sfully!n");范例2:利用open函数创建新文件 open_creat.c#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>void main()int fd;/*文件描述符*/fd = open("/home/test1.c",O_RDWR | O_CREAT,0755);if(fd<0) printf("Open file fali!n");else printf("Open file sucessfully!
4、n");6 创建文件creat 在一书中,找到函数名利用man creat查找函数的帮助文件编写程序代码#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <fcntl.h>void main()int fd;fd = creat("/home/test2.c",0621);if(fd<0) printf("create file fail!n");else printf("Create fi
5、le successfully!n");7 在一书中,找到函数名 man close 查找函数原型 编写程序代码close.c#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <fcntl.h>void main()int fd;fd = open("/home/test2.c",O_RDWR | O_CREAT,0755);if(fd<0) printf("create file fail!n");
6、else printf("Create file successfully!n"); int ret;ret = close(fd);if(ret = 0) printf("File has been closed!n");else printf("Fail to close!n");8 首先查找一下读文件的函数read()man所查询的是一个手册,首先从章节一里面找关键字,如果没有找到,再到后续章节中找。man 2 readman 章节一 命令 章节二 系统调用 章节三 库函数范例:#include <fcntl.h>v
7、oid main()int fd;fd = open("/home/test1.c",O_RDWR | O_CREAT,0755);if(fd<0) printf("create file fail!n");else printf("Create file successfully!n");char buf10;/*定义一个数组,有10个空间;用来存放读取出的数据*/ ssize_t count; count = read(fd,buf,5);/*将读取出的字符存放到buf指向的空间里面。*/if(count=-1) print
8、f("Read fail!");else printf("Read %d Bytes.n",count);buf5='0' /*这样,读取后的内容无乱码字符出现。*/printf("buf is %s.n",buf); /*打印字符串*/ int ret;ret = close(fd);if(ret = 0) printf("File has been closed!n");else printf("Fail to close!n");9 写文件 找到写文件对应的函数write(
9、) man 2 writewrite.c#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <fcntl.h>void main()int fd;fd = open("/home/test2.c",O_RDWR | O_CREAT,0755);if(fd<0) printf("create file fail!n");else printf("Create file successfully!n&quo
10、t;); char *buf = "987654321"ssize_t count;count = write(fd,buf,7);if(count=-1) printf("Write fail!");else printf("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");e
11、lse printf("Fail to close!n");10 定位文件lseek()在此之前,先实验一下,能否在文件写入之后,直接读取刚才写入的内容?实验验证,没有读到数据。这就是我们要给大家介绍的文件读写指针。当打开文件时,指针位于文件头部,然而,当对文件进行读或者写操作后,文件指针将发生变化。介绍,如何设置指针的位置。范例:#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <fcntl.
12、h>void main()int fd;fd = open("/home/test2.c",O_RDWR | O_CREAT,0755);if(fd<0) printf("create file fail!n");else printf("Create file successfully!n"); char *buf = "987654321"ssize_t count;count = write(fd,buf,7);if(count=-1) printf("Write fail!"
13、);else printf("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");else printf("The distance to file_head is %d Bytes.n",ref); char buf_r10;/*定义一个数组,有10个空间;用来存放读取出的数据*/ s
14、size_t count_r; count_r = read(fd,buf_r,5);/*将读取出的字符存放到buf指向的空间里面。*/if(count_r=-1) printf("Read fail!");else printf("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(&q
15、uot;File has been closed!n");else printf("Fail to close!n");11 复制文件描述符 dup()可以利用新复制的文件描述符fd_new来操作oldfd所指定的文件。就好比一个人有两个身份证号码。/*利用dup()函数复制后的文件描述符不同于原来的,但共同指向同一个文件*/范例:dup.c#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include
16、<fcntl.h>void main()int fd;fd = open("/home/test2.c",O_RDWR | O_CREAT,0755);if(fd<0) printf("create file fail!n");else printf("Create file successfully!n"); char *buf = "987654321"ssize_t count;count = write(fd,buf,7);if(count=-1) printf("Write f
17、ail!");else printf("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");else printf("The distance to file_head is %d Bytes.n",ref); int newfd;newfd = dup(fd);if(newf
18、d=-1) printf("Fail to copy the old fd.n");else printf("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!");else printf("Read %d Bytes.n"
19、;,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");printf("The oldfd and the newfd are %d & %d.n",fd,newfd); else printf("Fail to close!n");13 综合实
20、例编写文件复制程序cp 参数1 参数2源文件 、目标文件将源文件的所有内容复制到目标文件中;思路:打开源文件,打开目标文件;读取源文件的数据,将数据写入目标文件;open read write查阅编写的函数手册。源文件是什么、目标文件是什么?通过main()函数的参数传进去。编写文件复制程序copy.c#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <fcntl.h>void main(int argc,char *argv)/*argv0存放命令的名字,argv1存放参数1,argv2存放参数2*/
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026安徽六安市舒城县晓天镇招考村级后备干部15人备考题库含答案详解(黄金题型)
- 2026年物流开发教育合作协议
- 2026山东临沂市郯城县司法雇员第二次招聘29人备考题库附答案详解(培优a卷)
- 2026四川资阳市安岳县县属国有企业招聘25人备考题库附答案详解(黄金题型)
- 2026浙江绍兴市柯桥区慈善总会招聘1人备考题库及答案详解(全优)
- 2026江苏南京大学YJ20260595电子科学与工程学院特任副研究员招聘1人备考题库附答案详解(完整版)
- 2026宁波富达股份有限公司招聘2人备考题库附答案详解(预热题)
- 2026广东深圳市宝安区翻身实验学校(西校区)诚聘初中历史教师1人备考题库及答案详解(夺冠)
- 2026山东潍坊市临朐县招聘教师58人备考题库及答案详解(名校卷)
- 2026浙江嘉兴市海宁颐和医养健康管理有限公司招聘5人备考题库及答案详解(典优)
- 江苏省2026年中职职教高考文化统考数学试卷及答案
- 26年类器官药敏联合基因检测用药
- 2026年西安建筑科技大学《绿色建筑学报》编辑部招聘(3人)笔试参考题库及答案解析
- 2026年北京市东城区高三二模生物试卷(含答案)
- 2026滁州市轨道交通运营有限公司第一批次校园招聘21人备考题库及完整答案详解一套
- T/CSMTNY 003-2026管输掺氢天然气质量分析与流量计量技术指南
- (2026年)压疮的预防及护理课件
- 2026届广西南宁市4月高中毕业班质量调研英语试卷(含答案无听力音频无听力原文)
- 侍茄师(雪茄服务师)初级测试题
- DB3717∕T 30-2025 芍药鲜切花采后处理技术规程
- 初中地理教师教学能力提升培训
评论
0/150
提交评论