in ,

The way to Seize the Output of a Linux Command in C++


Final Replace: Jul 11, 2021


Okay, I admit this can be a fairly obscure one, however this got here up for a activity at work at this time, and I had forgotten how to do that. I Googled round a bit and located many solutions I both didn’t perceive completely or simply appeared too sophisticated for what I used to be attempting to perform. I had completed this with the enhance libraries earlier than, however I didn’t have them on this machine, so I needed a clear, stdlib kind answer.

Need to study extra about C++? Take a look at this C++ fundamentals course. It covers C++ 17!

I dug round and located this snippet of code on my exhausting drive and put one thing collectively to make it work. I didn’t write it, and I don’t bear in mind the place it got here from, so I can’t give credit score for it, however it works like a appeal:

(examined utilizing G++ v. 4.4.4)

string GetStdoutFromCommand(string cmd) {

  string information;
  FILE * stream;
  const int max_buffer = 256;
  char buffer[max_buffer];
  cmd.append(" 2>&1");

  stream = popen(cmd.c_str(), "r");

  if (stream) {
    whereas (!feof(stream))
      if (fgets(buffer, max_buffer, stream) != NULL) information.append(buffer);
    pclose(stream);
  }
  return information;
}

As you possibly can see, this can be a operate, and also you move the command as a string to that operate. It’s easy to make use of. As a observe, you possibly can run system instructions from inside C utilizing system.

The issue with that is, the command is usually run first, earlier than any output instructions and the operate returns an integer indicating success or failure, however not the output of the string. The operate above fixes that for you.

What it does is creates a buffer, opens up a read-only stream, runs the command, and captures the output, stuffs it into the buffer, then returns it as a string.

Right here is how we put it to make use of:

int major() {

  string ls = GetStdoutFromCommand("ls -la");

  cout << "LS: " << ls << endl;

  return 0;
}

Needless to say there isn’t a actual checking right here, and you must take a look at the outputs of the instructions fastidiously. As an illustration, after I used it at this time, I had lengthy instructions the place I piped the output into into grep and lower, and many others. If I bought surprising output, it prompted an issue that hung the executable. So that you wish to ensure that and validate your enter and output as you go alongside, don’t simply go working a bunch of instructions and dealing on the output with out some good error checking.

Right here is the complete code:

#embrace <iostream>
#embrace <stdio.h>
#embrace <stdlib.h>

utilizing namespace std;

string GetStdoutFromCommand(string cmd) {

  string information;
  FILE * stream;
  const int max_buffer = 256;
  char buffer[max_buffer];
  cmd.append(" 2>&1");

  stream = popen(cmd.c_str(), "r");
  if (stream) {
    whereas (!feof(stream))
      if (fgets(buffer, max_buffer, stream) != NULL) information.append(buffer);
    pclose(stream);
  }
  return information;
}

int major() {

  string ls = GetStdoutFromCommand("ls -la");
  cout << "LS: " << ls << endl;
  return 0;
}

As I mentioned, that is fairly obscure, however I figured if I had this downside, others would possibly as nicely. I assumed I’d throw it on the market. I hope it helps.

Completely happy Coding!


If you happen to’d prefer to study extra, right here’s a fantastic C++ fundamentals course.

Superior C++ builders desirous to stage up ought to try Excessive-performance computing in C++








Supply hyperlink

What do you think?

Written by admin

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

GIPHY App Key not set. Please check settings

*HOT* Lowe’s: FREE Flowers or Plant for Mom’s Day!

Google Data Native Panel With Typically Searched Collectively