Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Catching output from a command

Status
Not open for further replies.

OttSens

Programmer
Dec 13, 2001
80
US
Hi All

I'm triing to catch the output from a g++ command
from my ui. I'm using popen to get a file pointer
from which I read output. The catch is that this
works fine with regular shell commands like
"ps -ax\n" but with g++ the output goes to stderr.
As near as I can tell there is no way to read this
with popen. Yes I can pipe all the output to a file
and then read that but it's a kludge and I think
there must be a better way.

code
(This is c code really that lives in a c++ app.)


void execCompile(char* command,Fl_Browser* b)
{
FILE* read_fp;
char* buffer[BUFSIZ+1];
char* back_wash=(char*)malloc(20000); //big ass bunch of errors.
memset(buffer,'\0',sizeof(buffer));
memset(back_wash,'\0',20000);
int chars_read;
read_fp=popen(command,"r");
if(read_fp!=NULL)
{
char* place=back_wash;
chars_read=fread(buffer,sizeof(char),BUFSIZ,read_fp);
memcpy((void*)place,buffer,chars_read);
place+=chars_read;
while(chars_read>0)
{
buffer[chars_read-1]='\0';
printf("READING %d: %s",chars_read,buffer);
memcpy((void*)place,buffer,chars_read);
chars_read=fread(buffer,sizeof(char),BUFSIZ,read_fp);
}
if(pclose(read_fp)==-1)
printf("pclose failed\n");
printf("**********************************************************\n");
printf("%s",back_wash);
free(back_wash);
return;
}
printf("ERROR opening pipe\n");
return;
}
 
Hi,
I think you problem is most compiler write their output to STDERR not STDOUT. Therefore opening a PIPE to the process only captures STDOUT.

Why don't you just put the output ( and error output ) into a file and then read the file after the command is complete.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top