亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: 請幫我看個popen,pclose的報錯 [打印本頁]

作者: hmchzb19    時間: 2016-09-01 21:25
標(biāo)題: 請幫我看個popen,pclose的報錯
我看了這里的代碼,錯的離譜的很啊
http://crasseux.com/books/ctutorial/Programming-with-pipes.html#Programming%20with%20pipes

  1. SYNOPSIS
  2.        #include <stdio.h>

  3.        ssize_t getline(char **lineptr, size_t *n, FILE *stream);

  4.        ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
復(fù)制代碼
getdelim 的原型第三個參數(shù)是int, 這里面怎么能用 "\n\n" 2個換行做分割。

我改了下這個代碼,還是不對,關(guān)鍵是我還不知道錯在哪里,請大家指點下。

  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <errno.h>
  5. #include <string.h>

  6. int main()
  7. {
  8.     FILE *ps_pipe;
  9.     FILE *grep_pipe;

  10.     size_t bytes_read;
  11.     size_t nbytes=100;
  12.     char *my_string;

  13.     //open our two pipes
  14.     ps_pipe=popen("ps -A","r");
  15.     grep_pipe=popen("grep init","w");

  16.     //check that pipes are non-null,therefore open
  17.     if((!ps_pipe) || (!grep_pipe)){
  18.         fprintf(stderr,"One or both pipes failed.\n");
  19.         return EXIT_FAILURE;
  20.         }

  21.     //read from ps_pipe until two newlines
  22.     my_string=(char *)malloc(nbytes+1);
  23.     bytes_read=getline(&my_string,&nbytes,ps_pipe);
  24.    
  25.     int errno;
  26.     //closing ps_pipe,checking for errors
  27.     if(pclose(ps_pipe)!=0){
  28.         perror("Could not run 'ps', or other error.\n");
  29.         }

  30.     //send output of 'ps -A' to 'grep init' with two newlines
  31.     fprintf(grep_pipe, "%s\n",my_string);
  32.    
  33.     int ret;
  34.     char error[100];
  35.     //close grep_pipe,checking for errors
  36.     if((ret=pclose(grep_pipe))!=0){
  37.         printf("ret is %d",ret);
  38.         perror("pclose()");
  39.         printf("the pclose errored with %s",strerror(ret));
  40.         }

  41.     return 0;
  42.     }
復(fù)制代碼
現(xiàn)在運行報錯是這個
  1. pclose(): Success
  2. ret is 256the pclose errored with Unknown error 256
復(fù)制代碼







歡迎光臨 Chinaunix (http://72891.cn/) Powered by Discuz! X3.2