Return of system(); c++

Asked

Viewed 380 times

1

Someone could help me, for a while now I have this doubt, regarding how to pass to a variable the return of the function System(); This function executes a certain command in the console, and brings the results on the screen. If I for example wanted to store in a variable type string, the following output:

string hostname = system("whoami");

And make the contents contained therein hostname of the machine.

  • 1

    The return of the system function is an int with a value of 0 if everything went well (depends on the implementation) or a non-zero value if the parameter is NULL.

2 answers

1


Maybe what you need is the function fopen instead of system. You can do the following:

FILE* output_file = nullptr;
char buffer[1024];

output_file = popen("whoami", "r");

if(output_file)
{
    int i = 0;
    char c;

    while((c = fgetc(output_file)) != EOF)
    {
        buffer[i] = c;
        i++;
    } 
    buffer[i] = '\0';

    std::cout << buffer << '\n';
}

The popen function saves the output of the command to a file, and this file is returned by the function. Then just save the contents of the file to the buffer.

  • Great, it is exactly what I need, could explain me more about the nullptr, because in my code it says that it was not declared

  • 1

    nullptr is the same as NULL, introduced in c++11. Note that unlike NULL, nullptr is not a macro, it is a reserved word and I recommend you use it. The error must be because c++11 is not activated. Go quick and active, modern c++ is much better.

1

Just to clarify a little bit about the function system, because the use you have shown is not correct:

string hostname = system("whoami");

The function system executes a given command but does not return the results shown on the screen but an integer. I quote the documentation about the meaning of this returned integer:

If command is a null Pointer, the Function Returns a non-zero value in case a command Processor is available and a zero value if it is not.

If command is not a null Pointer, the value returned depends on the system and library implementations, but it is generally expected to be the status code returned by the called command, if supported.

In free translation:

If the command is a NULL pointer, the function returns a non-zero value if the command processor is available and zero otherwise.

If the command is not a NULL pointer, the value returned will depend on the operating system and its implementation, but generally it is expected to be a state code returned by the executed command, if supported.

In conclusion, the value returned is a number, which depends on the implementation, but is usually a code. So this function does not return what it expects, although the result of executing the command is visible in the console after calling the function system.

To get the result of executing the command as text and save to a variable you can do as it is in reply from @user72726

Browser other questions tagged

You are not signed in. Login or sign up in order to post.