当前位置:文档之家› 系统调用方式文件编程题库

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

Linux文件编程函数一简述几个基本知识——1 Linux应用程序编程所用到的函数,主要有两种方式提供:系统调用方式函数库方式2 如何学习这些函数?三步学习法:第一步:借助工具书,查找函数名;《Unix环境高级编程》第二步:在Linux系统中,利用man命令查看函数信息,并填写函数学习手册。

第三步:实践,编写代码。

3 VI概念——文件描述符性质:一个数字特别含义:其功能类似于身份证号码,通过身份证号码,可以将对应的公民;在Linux系统中,每一个打开的文件,都对应一个数字,通过这个唯一的数字,可以找到这个打开的文件,并对其进行操作,比如读、写等。

二首先学习系统调用方式提供的函数——4 学习以下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");elseprintf("Open file sucessfully!\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");elseprintf("Open file sucessfully!\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");elseprintf("Create file 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");elseprintf("Create file successfully!\n");int ret;ret = close(fd);if(ret == 0)printf("File has been closed!\n");elseprintf("Fail to close!\n");}8 首先查找一下读文件的函数read()man所查询的是一个手册,首先从章节一里面找关键字,如果没有找到,再到后续章节中找。

man 2 readman 章节一命令章节二系统调用章节三库函数范例:#include <fcntl.h>void main(){int fd;fd = open("/home/test1.c",O_RDWR | O_CREAT,0755);if(fd<0)printf("create file fail!\n");elseprintf("Create file successfully!\n");char buf[10];/*定义一个数组,有10个空间;用来存放读取出的数据*/ssize_t count;count = read(fd,buf,5);/*将读取出的字符存放到buf 指向的空间里面。

*/if(count==-1)printf("Read fail!");elseprintf("Read %d Bytes.\n",count);buf[5]='\0'; /*这样,读取后的内容无乱码字符出现。

*/printf("buf is %s.\n",buf); /*打印字符串*/int ret;ret = close(fd);if(ret == 0)printf("File has been closed!\n");elseprintf("Fail to close!\n");}9 写文件找到写文件对应的函数write()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");elseprintf("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);int ret;ret = close(fd);if(ret == 0)printf("File has been closed!\n");elseprintf("Fail to close!\n");}10 定位文件lseek()在此之前,先实验一下,能否在文件写入之后,直接读取刚才写入的内容?实验验证,没有读到数据。

这就是我们要给大家介绍的文件读写指针。

当打开文件时,指针位于文件头部,然而,当对文件进行读或者写操作后,文件指针将发生变化。

介绍,如何设置指针的位置。

范例:#include <stdio.h>#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");elseprintf("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 file_head is %d Bytes.\n",ref);char buf_r[10];/*定义一个数组,有10个空间;用来存放读取出的数据*/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_r[5]='\0'; /*这样,读取后的内容无乱码出现。

*/printf("buf_r is %s.\n",buf_r); /*打印读出的字符串*/int ret;ret = close(fd);if(ret == 0)printf("File has been closed!\n");elseprintf("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 <fcntl.h>void main(){int fd;fd = open("/home/test2.c",O_RDWR | O_CREAT,0755);if(fd<0)printf("create file fail!\n");elseprintf("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 file_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_r[10];/*定义一个数组,有10个空间;用来存放读取出的数据*/ssize_t count_r;count_r = read(newfd,buf_r,5);/*将读取出的字符存放到buf指向的空间里面。

相关主题