Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 891721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:56:01+00:00 2026-05-15T13:56:01+00:00

Suppose I do a system(ps-C nautilus); how do I return the result of this

  • 0

Suppose I do a system("ps-C nautilus"); how do I return the result of this function in char* ?

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T13:56:02+00:00Added an answer on May 15, 2026 at 1:56 pm

    If your compiler supports some variant of popen() you can redirect the output fairly easily.

    Caveats:

    1. The code below was written on a Windows box, which uses _popen() instead of popen(). It is not hard to convert for *nix.
    2. The code below has no error handling. It is just intended to illustrate the technique. You must add error checking before using it in production.
    3. This all runs in one thread. If the program was likely to run for a long time, you might need to use a second thread to read its output while it runs.
    4. I think popen() is POSIX, which means it will work on most *nix boxes, but it is not standard C, so should not be considered portable.
    char *RunProg(const char *szCmdLine)
    {
      size_t nSize=512;
      size_t nUsed=0;
      char *szOut = malloc(nSize);
      FILE *fp = _popen(szCmdLine, "r");
      char szBuff[128];
      while( !feof( fp ) )
      {
          if( fgets( szBuff, sizeof(szBuff), fp ) != NULL )
          {
              nUsed += strlen(szBuff);
              if (nUsed >= nSize)
              {
                  nSize += nUsed;
                  szOut = realloc(szOut, nSize);
              }
              strcat(szOut, szBuff);
          }
      }
      _pclose(fp);
      return szOut;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose there is a System A which gives some output. This output is used
string EV_HOME = System.Environment.GetEnvironmentVariable(EV_HOME); string myFilePath = EV_HOME + \\MyFolder\\MySubFolder; Suppose EV_HOME return C:\myprogram\leanrning\anotherfolder
Hopefully, this fictitious example will illustrate my problem: Suppose you are writing a system
In common-lisp, I want to implement a kind of reference system like this: Suppose
suppose if a program make a operating system function call and I have to
Suppose you have a system on the other side of a network that sends
Suppose you have a messaging system built in PHP with a MySQL database backend,
Suppose I have a system where I have metadata such as: table: ====== key
Suppose I have a method like so: public byte[] GetThoseBytes() { using (System.IO.MemoryStream ms
Suppose an object travels in a parabola in 2D coordinate system from P to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.