unix高级环境编程 例子 代码实现练习 第三章:文件I/O

来源:互联网 发布:趣味编程Scratch ppt 编辑:程序博客网 时间:2024/05/16 03:51

程序清单 3-1“ 测试能否对标准输入设置偏移量

/** * 程序清单 3-1“ 测试能否对标准输入设置偏移量 P51 * * zy: * 对lseek函数加深认识。 *  * 书中第一个例子/etc/motd是进入tty的一段文字,所以可以随意调整 * 书中第二个例子表示对管道进行lseek,但是不可以的 * 书中第三个例子,我相应的目录下没有找到该文件,我使用mkfifo创建了一个, * 但是对其使用我们的程序是无效的,就是什么也不会出现。所以不知道我用mkfifo创建的东西 * 和书中是不是一个东西。 *  */#include "apue.h"#include <stdlib.h>#include "error.c"int main(void) {if(lseek(STDIN_FILENO,0,SEEK_CUR)==-1){//必须测试其是否等于-1,因为某些设备的偏移量可以是负的printf("cannot seek \n ");}else{printf("seek ok\n ");}exit(0);}


结果:
asd@asd-desktop:~/workspace/test/src$ !1029./a.out </etc/motd seek ok asd@asd-desktop:~/workspace/test/src$ !1030cat </etc/motd | ./a.out cannot seek  asd@asd-desktop:~/workspace/test/src$ !1034mkfifo fifomkfifo: cannot create fifo `fifo': File existsasd@asd-desktop:~/workspace/test/src$ lltotal 36drwxrwxr-x 2 asd asd  4096 Mar  7 08:45 ./drwxrwxr-x 3 asd asd  4096 Mar  7 08:00 ../-rwxrwxr-x 1 asd asd 11142 Mar  7 08:45 a.out*-rw-r--r-- 1 asd asd  4736 May 28  2005 apue.h-rw-r--r-- 1 asd asd  2021 May 24  2005 error.cprw-rw-r-- 1 asd asd     0 Mar  7 08:40 fifo|-rw-rw-r-- 1 asd asd   383 Mar  7 08:36 test.casd@asd-desktop:~/workspace/test/src$ ./a.out < fifo

程序清单 3-2 创建一个具有空洞的文件

/** * 程序清单 3-2 创建一个具有空洞的文件 * * zy: * 主要是证明有空洞的文件所占有的磁盘块数要少,虽然字节数是一样多的 * od特殊的形式查看文件内容,-c是以ASCII码的形式 */#include "apue.h"//fcntl.h有些操作系统特有文件打开操作比文件锁功能增强功能//stdio.h文件标准操作移植性好性能效率也都可以#include <fcntl.h>#include "error.c"char buf1[]="abcdefghij";char buf2[]="ABCDEFGHIJ";int main(void) {int fd;if((fd=creat("file.nohole",FILE_MODE))< 0){err_sys("create error");}if(write(fd,buf1,10)!=10){err_sys("buf1 write error!");}if(lseek(fd,16384,SEEK_SET)==-1){err_sys("lseek error!");}if(write(fd,buf2,10)!=10){err_sys("buf2 write error!");}exit(0);}

结果:

asd@asd-desktop:~/workspace/test/src$ ls -l ./file.hole-rw-r--r-- 1 asd asd 16394 Mar  7 09:11 ./file.holeasd@asd-desktop:~/workspace/test/src$ od -c file.hole 0000000   a   b   c   d   e   f   g   h   i   j  \0  \0  \0  \0  \0  \00000020  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0*0040000   A   B   C   D   E   F   G   H   I   J0040012

程序清单 3-3 将标准输入复制标准输出

/** * 程序清单 3-3 将标准输入复制标准输出 P55 * * zy: * 使用了P16的time -p命令来查看一个程序运行的时钟时间、用户CPU时间、系统CPU时间 * 我们检阅了缓冲区大小对复制一个文件的影响 * 我第一次使用的缓冲区大小和:4096 * 我第二次使用了:512 * 结果如下:可以看出缓冲区大小对复制文件产生的影响 * 更多数据请参考书 */#include "apue.h"#include "error.c"#define BUFFSIZE 512int main(void) {int n;char buf[BUFFSIZE];while((n=read(STDIN_FILENO,buf,BUFFSIZE))>0){if(write(STDOUT_FILENO,buf,n)!=n){err_sys("write error");}}if(n<0){err_sys("read error");}exit(0);}


结果:
asd@asd-desktop:~/workspace/test/src$ time -p ./a.out < /home/asd/Downloads/UNIX环境高级编程_第二版中文.pdf > /dev/null real 0.47user 0.00sys 0.01asd@asd-desktop:~/workspace/test/src$ gcc test.c asd@asd-desktop:~/workspace/test/src$ time -p ./a.out < /home/asd/Downloads/UNIX环境高级编程_第二版中文.pdf > /dev/null real 0.01user 0.00sys 0.01asd@asd-desktop:~/workspace/test/src$ 

程序清单 3-4 对于指定的描述符打印文件标志

/** * 程序清单 3-4 对于指定的描述符打印文件标志 P64 * * zy: * 更多fcntl的功能请查看P63 * 其中比较难理解的是第三个和第四个。 *./a.out 2 2>>temp.foo 意思:执行 ./a.out 2 命令 通过 2>>temp.foo 命令标准错误输出添加 temp.foo 文件尾部打印结果是:write only, append上面打印结果来来自标准输出,而我们只是改变了FD2的错误输出,但是并没有错误输出任何东西。不要以为write only是temp.foo的内容./a.out 5 5<>temp.foo执行 ./a.out 5 命令 打印5号文件描述符的情况,并通过 5<>temp.foo 将temp.foo 文件读写方式打开,其文件描述符为5。 * */#include "apue.h"#include "error.c"#include <fcntl.h>#define BUFFSIZE 512int main(int argc, char *argv[]) {int val;if(argc != 2){err_quit("usage: a.out <descriptor#>");}if((val=fcntl(atoi(argv[1]),F_GETFL,0))<0)//F_GETFL返回文件的读写状态err_sys("fcntl error for fd %d",atoi(argv[1]));switch (val & O_ACCMODE) {case O_RDONLY:printf("read only");break;case O_WRONLY:printf("write only");break;case O_RDWR:printf("write read");break;default:err_dump("unknown access mode");break;}if(val&O_APPEND){printf(", append");}if(val & O_NONBLOCK){printf(", nonBlocking");}#if defined (O_SYNC)if(val & O_SYNC){printf(", synchronous writes");}#endif#if !defined (_POSIX_C_SOURCE) && defined (O_FSYNC)//_POSIX_C_SOURCE表示系统对POSIX的支持程度的。if(val & O_FSYNC){printf(", synchronous writes");}#endifputchar('\n');exit(0);}

输出结果和书上一致。

程序清单 3-5 对一个文件符打开一个或者多个文件状态标志 P64

/** * 程序清单 3-5 对一个文件符打开一个或者多个文件状态标志 P64 * * zy: * 这个程序只是一个被调用的函数 * 比如, * 执行函数: * set_fl(STDOUT_FILENO,O_SYNC); * 也就相当于打开了同步写的标志,另一个函数是关闭的意思。 */#include "apue.h"#include "error.c"#include <fcntl.h>void set_fl(int fd,int flags){//flags就是要准备打开的标志int val;if((val= fcntl(fd,F_GETFL,0))<0){err_sys("fcntl F_GETFL error");}val |=flags; //与标志表示打开一个if(fcntl(fd,F_SETFL,val)<0){err_sys("fcntl F_SETFL error");}}void clr_fl(int fd,int flags){//flags就是要关闭的标志int val;if((val= fcntl(fd,F_GETFL,0))<0){err_sys("fcntl F_GETFL error");}val &=~flags; //将这个标志关闭if(fcntl(fd,F_SETFL,val)<0){err_sys("fcntl F_SETFL error");}}



0 0
原创粉丝点击