dup和dup2以及cat函数函数
今日命令:
more filenamen //分屏顯示filename 內(nèi)容 od -c filename //使用二進制形式查看文件內(nèi)容dup和dup2也是兩個非常有用的調(diào)用,它們的作用都是用來復(fù)制一個文件的描述符。它們經(jīng)常用來重定向進程的stdin、stdout和stderr。這兩個函數(shù)的原形如下:
#include <unistd.h> int dup( int oldfd ); int dup2( int oldfd, int targetfd );利用函數(shù)dup,我們可以復(fù)制一個描述符。傳給該函數(shù)一個既有的描述符,它就會返回一個新的描述符,這個新的描述符是傳給它的描述符的拷貝。這意味著,這兩個描述符共享同一個數(shù)據(jù)結(jié)構(gòu)。例如,如果我們對一個文件描述符執(zhí)行l(wèi)seek操作,得到的第一個文件的位置和第二個是一樣的。下面是用來說明dup函數(shù)使用方法的代碼片段:
int fd1, fd2; ... fd2 = dup( fd1 );dup2函數(shù)跟dup函數(shù)相似,但dup2函數(shù)允許調(diào)用者規(guī)定一個有效描述符和目標(biāo)描述符的id。在使用dup2的時候若第二個參數(shù)的描述符targetfd已經(jīng)打開則現(xiàn)將其關(guān)閉, dup2函數(shù)成功返回時,目標(biāo)描述符(dup2函數(shù)的第二個參數(shù))將變成源描述符(dup2函數(shù)的第一個參數(shù))的復(fù)制品,換句話說,兩個文件描述符現(xiàn)在都指向同一個文件,并且是函數(shù)第一個參數(shù)指向的文件。下面我們用一段代碼加以說明:
//復(fù)制文件描述符并向文件寫數(shù)據(jù)
#include <unistd.h> #include <fcntl.h> #include <stdlib.h> #include <stdio.h> #include <string.h>void main() {int fd,newfd;char *bufFD="Advanced Programming! write by fd\n";char *bufNewFD="Advanced Programming! write by NewFD\n";fd = open("test.txt",O_RDWR|O_CREAT,0644);if(fd==-1){printf("open file error%m\n");exit(-1);}//開始復(fù)制了newfd = dup(fd);//使用fd寫write(fd,bufFD,strlen(bufFD));close(fd);//使用newfd寫write(newfd,bufNewFD,strlen(bufNewFD));if(close(newfd)==-1){printf("close error\n");}exit(0); }實現(xiàn)簡單cat命令:實現(xiàn)的是無論輸入多少參數(shù)都能將其轉(zhuǎn)化為標(biāo)準(zhǔn)輸出輸出出來。
#include "io.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fcntl.h>int main(int argc, char * argv[]) {int fd_in = STDIN_FILENO;int fd_out = STDOUT_FILENO;int i;for(i = 1; i < argc; i ++){ //寫上fro循環(huán)是為了無論輸入幾個命令行參數(shù),都能夠?qū)⑵滢D(zhuǎn)換為標(biāo)準(zhǔn)輸出輸出出來fd_in = open(argv[i],O_RDONLY);if(fd_in < 0){perror("open error");continue;}copy(fd_in,fd_out);close(fd_in);}if(argc == 1){ copy(fd_in,fd_out);}return 0; }io.c文件
#include "io.h" #include <unistd.h> #include <fcntl.h> #include <errno.h> #include <string.h> #include <stdlib.h> #include <stdio.h>#define BUFFER_LEN 1024void copy(int fdin, int fdout) {char buffer[BUFFER_LEN];ssize_t size;while(( size = read(fdin, buffer, BUFFER_LEN )) > 0){ printf("current %ld\n",lseek(fdin,0L,SEEK_CUR));if( write( fdout, buffer, size) != size){fprintf(stderr,"write error: %s\n",strerror(errno));exit(1);}}if(size < 0){fprintf(stderr, "read error: %s\n",strerror(errno));exit(1);}}io.h頭文件
#ifndef __IO_H #define __IO_Hextern void copy(int fdin, int fdout);#endif使用dup函數(shù),打造升級版cat命令:
#include "io.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fcntl.h>/* *bin/mcat + iotec.txt (+為輸入重定向) *bin/mcat - iotec.txt(-為輸出重定向) */int main(int argc, char * argv[]) {int fd_in = STDIN_FILENO;int fd_out = STDOUT_FILENO;int flag = 0;int i;for(i = 1; i < argc; i ++){ //寫上fro循環(huán)是為了無論輸入幾個命令行參數(shù),都能夠?qū)⑵滢D(zhuǎn)換為標(biāo)準(zhǔn)輸出輸出出來if(!strcmp("+", argv[i])){fd_in = open(argv[++i],O_RDONLY);if(fd_in < 0){perror("open error");}//將標(biāo)準(zhǔn)輸入重定向到文件if(dup2(fd_in, STDIN_FILENO) != STDIN_FILENO){perror("dup2 error!");exit(1);}close(fd_in);}else if(!strcmp("-",argv[i])){fd_out = open(argv[++i], O_WRONLY | O_CREAT | O_TRUNC, 0777);if(fd_out < 0){perror("open fd_out error!");exit(1); }//將標(biāo)準(zhǔn)輸出重定向到文件if(dup2(fd_out, STDOUT_FILENO) != STDOUT_FILENO){perror("dup2 error");exit(1);}}else {flag = 1;;fd_in = open(argv[i], O_RDONLY);if(fd_in < 0){perror("open error!");exit(1);}if(dup2(fd_in ,STDIN_FILENO) != STDIN_FILENO){perror("dup2 error");exit(1);}copy(STDIN_FILENO,STDOUT_FILENO);close(fd_in);}}if(!flag){copy(STDIN_FILENO, STDOUT_FILENO);}return 0; }總結(jié)
以上是生活随笔為你收集整理的dup和dup2以及cat函数函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 作者:冯景华(1984-),男,国家超
- 下一篇: 【2016年第6期】基于大数据的移动互联