题目
编写一个程序,最多可接收3个命令行参数:
$ ./5-3 filename num-bytes [-x]
|
该程序打开指定的文件,然后每次写入一个字节的方式,向尾部追加num-bytes字节。缺省情况下,打开文件的标志应有O_APPEND,但若存在第三个命令行参数,那么就使用lseek到文件末尾再进行写入。最后运行这两个命令查看结果:
./5-3 f1 1000000 & ./5-3 f1 1000000 和 ./5-3 f1 1000000 -x & ./5-3 f1 1000000 -x
|
代码
#include <ctype.h> #include "tlpi_hdr.h" #include <stdbool.h> #include <sys/stat.h> #include <fcntl.h> int main(int argc, char *argv[]) { char ch; char byte='a'; bool x = FALSE; int fd=-1,cnt; if (argc < 3) { usageErr("filename num-bytes [-x]\n"); exit(EXIT_SUCCESS); } while ((ch = getopt(argc, argv, "x")) != -1) { switch (ch) { case 'x': printf("have x\n"); x = TRUE; break; default: usageErr("filename num-bytes [-x]\n"); break; } } if (x) { cnt=atoi(argv[3]); byte='x'; fd=open(argv[2],O_RDWR | O_CREAT , S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); if(fd==-1) { errExit("open"); } while(cnt) { lseek(fd,0,SEEK_END); write(fd,&byte,1); cnt--; } }else { cnt=atoi(argv[2]); fd=open(argv[1],O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); if(fd==-1) { errExit("open"); } while(cnt) { write(fd,&byte,1); cnt--; } }
exit(EXIT_SUCCESS); }
|
结果
扩展
我尝试修改了这个程序名为5-3.1,并且查看结果 | | 原程序 | 新程序 |
|-- |----------|----------| |无 -x |写入a |写入b |
|有 -x |写入x |写入y |