您好,欢迎来到钮旅网。
搜索
您的当前位置:首页linux 下管道文件操作

linux 下管道文件操作

来源:钮旅网
//linux 下有名管道的文件操作 #include #include #include #include #include #include #include

int main(int argc, char *argv[]) {

int n, fds[2] = {0}; char pbuf[100] = {0}; FILE *fp1, *fp2; if(argc != 2)

perror(\"Usage: a.out \"); if(!(fp1 = fopen(argv[1], \"r\")))

printf(\"can't open %s\", argv[1]); if(!(fp2 = fopen(\"./fpipe\", \"w+\"))) printf(\"can't write fpipe\"); if(pipe(fds)< 0)

perror(\"pipe error\"); pid_t pid = fork(); if(pid < 0)

perror(\"fork error\"); if(pid == 0){

close(fds[1]);

while(read(fds[0], pbuf, sizeof(pbuf)) != '\\0'){ fputs(pbuf, fp2);

memset(pbuf, 0, sizeof(pbuf)); }

close(fds[0]); } else {

close(fds[0]);

while(fgets(pbuf, sizeof(pbuf), fp1) != NULL){ n = strlen(pbuf);

if(write(fds[1], pbuf, n) != n) perror(\"write error to pipe\"); }

close(fds[1]); }

fclose(fp2); fclose(fp1);

return 0; }

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- niushuan.com 版权所有 赣ICP备2024042780号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务