Results 1 to 7 of 7
I understand I can use freopen to redirect stderror to a file; I can get my program working, however I do not want to be doing that.
I am running ...
- 09-02-2008 #1Just Joined!
- Join Date
- Sep 2008
- Posts
- 7
redirect stderr
I understand I can use freopen to redirect stderror to a file; I can get my program working, however I do not want to be doing that.
I am running on Ubuntu. doing some googling I understand os.popen4 require me to install libs in my system. I cannot be doing that as this program eventually run in an embeded system.
I found pstream (PStreams - POSIX Process Control in C++), but I am unable to use it according to their examples.
string str;
ipstream host("hostname");
getline(host, str); <=== always error as getline expects (char**, size_t*, FILE*), changed the syntax and I am still getting error as host isn't FILE *
did a quick Ubuntu and pstream search and I found I need to install the libs as well.
I am using popen to run some command, and wish to capture the stderr without directing it to a file. Is there a way to cleanly and easily do it?
eg:
FILE *fp = popen(command, "r");
while (fgets(buf, 511, fp))
{
...
}
- 09-02-2008 #2
if your just trying to send or redirect stderr why don't you try dup2(int old, int new)
- 09-03-2008 #3Just Joined!
- Join Date
- Sep 2008
- Posts
- 7
dup2 duplicate a file descriptor, how will that help in redirecting stderr?
have an example or link to help? I am not getting the use at this time.
- 09-03-2008 #4
stderr file descriptor is 2....I hope this helps
stdin = 0
stdout = 1
stderr = 2
- 09-03-2008 #5Just Joined!
- Join Date
- Sep 2008
- Posts
- 7
Thanx, I read up on this thread on dup but somehow I am not able to get it working. The stderr is not redirected. what could be the issue here.
Yahoo! Groups
FILE *fp = popen(command, "r");
int d = fileno(fp);
dup2(d,fileno(stdout));
dup2(d,fileno(stderr);
while (fgets(buf, 511, fp))
{
....
}
- 09-03-2008 #6Just Joined!
- Join Date
- Sep 2008
- Posts
- 7
I can get popen( "command1 2>&1", "r" ) to redirect stderr
however if there is a string of commands
eg:
"command1;command2;command3" I will need to perform a insert of " 2>&1" according.
Is there a easier to get dup2, freopen etc so that I don't have to do " 2>&1"?
- 09-03-2008 #7
I never used popen before but if your using pipes and redirection this is a good example of redirection....just compile this program then run it:
./program ps wc
and it will redirect the bash command ps into wc
Code:#include <unistd.h> #include <stdlib.h> enum PIPES {READ, WRITE}; int main(int argc, char**argv) { if (argv[2]) { int hpipe[2]; pipe(hpipe); if (fork()) { close(hpipe[WRITE]); dup2(hpipe[READ], 0);//stdin = 0 close(hpipe[READ]); execlp(argv[2],argv[2],NULL); } else { close(hpipe[READ]); dup2(hpipe[WRITE], 1);//stdout = 1 close(hpipe[WRITE]); execlp(argv[1],argv[1],NULL); } } exit(EXIT_SUCCESS); }


Reply With Quote