Linux console commands in C ++ (gcc compiler)

How can I give commands for the Linux console (Ubuntu) from my C ++ program and assign the value that my command reports to a string variable? Please give me an example in which the program gives a simple command "uname -a" to consolidate and record the result.

Sorry for my bad English, I know very little of it. I would be very pleased if someone writes their answer in Russian (if allowed). I was looking for the answer to my question in Russian resources, but did not find anything, you are my last hope.

+3
source share
2 answers

popen. , man popen ; Linux , .

, popen "" (), . , :

#include <stdio.h>
int main()
{
  FILE *f;
  char stuff[100];
  f = popen("uname -a", "r");
  fgets(stuff, 100, f);
  printf("%s", stuff);
  pclose(f);
}

; , (rus).

+3

, popen (3). popen , , .

+1

Source: https://habr.com/ru/post/1774727/


All Articles