1

I am using following code for extracting output of system command . I have not set path for "pic" in PATH variable. and i want to store output of command "which pic" and do not want to display it on console.

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int main ()
{
    FILE *fp;
  int status;
  char path[1035];
char *command = "which pic";


  /* Open the command for reading. */
  fp = popen(command, "r");
  if (fp == NULL) {
    printf("Failed to run command\n" );
    exit(0);
  }

  /* Read the output a line at a time - output it. */
  while (fgets(path, sizeof(path)-1, fp) != NULL) {
   cout<<"<<<<<<<<<<,"<<endl;
    printf("%s", path);
  }

  /* close */
  pclose(fp);

  return 0;
}

but it displaying following output in console :

which: no pic in(/usr/kerberos/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin)
3
  • You haven't really formulated a question, but one comment is that most likely, this output of which goes to standard error instead of standard output. You'll want to capture both. Commented Jan 3, 2013 at 8:53
  • cout<< and printf in same code! Commented Jan 3, 2013 at 9:00
  • @Angew You have solved my problem :) Commented Jan 3, 2013 at 9:09

1 Answer 1

1

Run "which pic 2>&1" as your command. You want to capture all output from which, including its errors (which are sent to stderr).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.