亚洲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
SYNOPSIS
#include <stdio.h>
ssize_t getline(char **lineptr, size_t *n, FILE *stream);
ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
復(fù)制代碼
getdelim 的原型第三個參數(shù)是int, 這里面怎么能用 "\n\n" 2個換行做分割。
我改了下這個代碼,還是不對,關(guān)鍵是我還不知道錯在哪里,請大家指點下。
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
int main()
{
FILE *ps_pipe;
FILE *grep_pipe;
size_t bytes_read;
size_t nbytes=100;
char *my_string;
//open our two pipes
ps_pipe=popen("ps -A","r");
grep_pipe=popen("grep init","w");
//check that pipes are non-null,therefore open
if((!ps_pipe) || (!grep_pipe)){
fprintf(stderr,"One or both pipes failed.\n");
return EXIT_FAILURE;
}
//read from ps_pipe until two newlines
my_string=(char *)malloc(nbytes+1);
bytes_read=getline(&my_string,&nbytes,ps_pipe);
int errno;
//closing ps_pipe,checking for errors
if(pclose(ps_pipe)!=0){
perror("Could not run 'ps', or other error.\n");
}
//send output of 'ps -A' to 'grep init' with two newlines
fprintf(grep_pipe, "%s\n",my_string);
int ret;
char error[100];
//close grep_pipe,checking for errors
if((ret=pclose(grep_pipe))!=0){
printf("ret is %d",ret);
perror("pclose()");
printf("the pclose errored with %s",strerror(ret));
}
return 0;
}
復(fù)制代碼
現(xiàn)在運行報錯是這個
pclose(): Success
ret is 256the pclose errored with Unknown error 256
復(fù)制代碼
歡迎光臨 Chinaunix (http://72891.cn/)
Powered by Discuz! X3.2