ImageVerifierCode 换一换
格式:DOCX , 页数:15 ,大小:17.44KB ,
资源ID:28827107      下载积分:10 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/28827107.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(系统调用方式文件编程题库.docx)为本站会员(b****8)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

系统调用方式文件编程题库.docx

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); else printf(Open file sucessfully!n); 范例2:利用open函数创建新文件 open_creat.c#include #include #include vo

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

4、eate file fail!n); else printf(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); else printf(Create file successfully!n); int ret; ret = c

5、lose(fd); if(ret = 0) printf(File has been closed!n); else printf(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); e

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

7、d); if(ret = 0) printf(File has been closed!n); else printf(Fail to close!n); 9 写文件 找到写文件对应的函数write() man 2 write write.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); else printf(Create file successfully!n)

8、; 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); else printf(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); else printf(Create file successfully!n);

10、 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); 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,

11、ref); char buf_r10;/*定义一个数组,有10个空间;用来存放读取出的数据*/ ssize_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)

12、 printf(File has been closed!n); else printf(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) prin

13、tf(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!); 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 t

14、o shift.n); else printf(The distance to file_head is %d Bytes.n,ref); int newfd; newfd = dup(fd); if(newfd=-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指向的空间里面。*/

15、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(File has been closed!n); printf(The oldfd and the newfd are %d & %d.n,fd,newfd); else printf(Fail to close!n);

16、 13 综合实例编写文件复制程序cp 参数1 参数2源文件 、目标文件将源文件的所有内容复制到目标文件中;思路:打开源文件,打开目标文件;读取源文件的数据,将数据写入目标文件;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 =

17、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 /

18、home/test3.c*/中间的数字是什么意思呢?是man的分卷号,原来man分成很多部分,分别是:1 用户命令, 可由任何人启动的。2 系统调用, 即由内核提供的函数。3 例程, 即库函数,比如标准C库libc。4 设备, 即/dev目录下的特殊文件。5 文件格式描述, 例如/etc/passwd。6 游戏, 不用解释啦!7 杂项, 例如宏命令包、惯例等。8 系统管理员工具, 只能由root启动。9 其他(Linux特定的), 用来存放内核例行程序的文档。n 新文档, 可能要移到更适合的领域。o 老文档, 可能会在一段期限内保留。l 本地文档, 与本特定系统有关的。 要查属于哪一部分的,就用哪一部分的编号在命令之前。

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1