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 7025649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:00:12+00:00 2026-05-28T00:00:12+00:00

My purpose of the program is to get the cmd output in a string

  • 0

My purpose of the program is to get the cmd output in a string but there is a strange problem :

string ChangeStatus()
{  status = exec("net stop mysql");
   cout<<status;
   return status;     
}

string exec(char* cmd) 
{
    pipe = _popen(cmd, "r");
    if (!pipe){
        sprintf(returnErrorMSG,"ERROR");
        return returnErrorMSG;
    }

    std::string result = "";
    while(!feof(pipe))
    {
        if(fgets(buffer, 128, pipe) != NULL)
        {
             result +=buffer;
        }
    }
    _pclose(pipe);
    return result;

}

My aim is to catch the output of the command (passed as a parameter to exec function, into a string variable –

Now the problem is Say, mysql is currently running : then if I call the function exec(“net stop mysql”),
it tries to stop the mysql and give the result in the result string which is returned from the function. The result string contains – “The Mysql service was started successfully” …. it is ok.

But if the mysql is currently running and I call exec(“net start mysql”), then it saying “The requested service has already been started” – which is totally justified. My point is this statement should be in the result string in the exec function. This time the result string is simply empty and the exec function returns a empty string. I need that output in the result string.

  • 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-28T00:00:12+00:00Added an answer on May 28, 2026 at 12:00 am

    In your case, the message goes to standard error stream, but popen() handles only standard output. To catch the message, you can use net stop mysql 2>&1 command or implement your version of popen() that will handle both standard output and standard error streams.

    Judging by the fact that you use _popen() instead of popen(), I assume that you may write not for Unix. So first option may not work in your OS.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My program that I am writing's purpose arose with this issue: There are two
It's quite possible i've misunderstood the purpose of the File dir argument in Runtime.exec(String
I've written several variations of a program. The purpose of the program is to
Here's the purpose of my console program: Make a web request > Save results
Trying to run a C++ program , I get segmentation faults which appear to
Is there a way to get the address of an object? This is for
When i try to output the string it doesnt output the text after the
Given this very simple Makefile : all: @mkdir -pv test/{a,b} I get this output
I'm trying to write a program that will get the current username and domain
The situation is my program's purpose is to update employee details. The user may

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.