Issuing system commands in Linux from C, C++
Not surprisingly, the command is still
system("whatever");
and the header is still stdlib.h
. That header file's name means "standard library", which means it's on every standard platform that supports C.
And yes, calling system()
is often a bad idea. There are usually more programmatic ways of doing things.
If you want to see how lsmod
works, you can always look-up its source code and see what the major system calls are that it makes. Then use those calls yourself.
A quick Google search turns up this link, which indicates that lsmod
is reading the contents of /proc/modules
.
How do I execute a Shell built-in command with a C function?
You should execute sh -c echo $PWD
; generally sh -c
will execute shell commands.
(In fact, system(foo)
is defined as execl("sh", "sh", "-c", foo, NULL)
and thus works for shell built-ins.)
If you just want the value of PWD
, use getenv
, though.
Try to execute command line codes from c++ linux
If you want to run a program and wait for it to finish before executing next line, take a look at this page and example code here: http://www.thegeekstuff.com/2012/03/c-process-control-functions/
But if you want to run gnome-terminal and execute a command in newly created window, do this:
system("gnome-terminal -x sh -c 'cd /tmp ; ls -la'");
How do I execute a command and get the output of the command within C++ using POSIX?
#include <cstdio>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <array>
std::string exec(const char* cmd) {
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}
Pre-C++11 version:
#include <iostream>
#include <stdexcept>
#include <stdio.h>
#include <string>
std::string exec(const char* cmd) {
char buffer[128];
std::string result = "";
FILE* pipe = popen(cmd, "r");
if (!pipe) throw std::runtime_error("popen() failed!");
try {
while (fgets(buffer, sizeof buffer, pipe) != NULL) {
result += buffer;
}
} catch (...) {
pclose(pipe);
throw;
}
pclose(pipe);
return result;
}
Replace popen
and pclose
with _popen
and _pclose
for Windows.
How many ways are there to execute system command in C program for windows
Standard C libraries give you only one way to execute external command in OS, so use int system(const char *command)
.
You can save output of this command to text file, and then read this file from you program.
For example:
#include <stdio.h>
#include <stdlib.h>
#define TMP_FILE_NAME "TMP_FOLDER_CONTENT.txt"
int main(int argc, char *argv[])
{
system("dir C:\* > "TMP_FILE_NAME);
FILE * fdir = fopen(TMP_FILE_NAME, "r");
char buff[100];
if (fdir)
{
while (1) {
if (fgets(buff, 100, fdir) == NULL) break;
printf("%s", buff);
}
}
fclose(fdir);
remove(TMP_FILE_NAME);
return 0;
}
Where dir
is a program to be executed, C:\*
- argument of the program, and >
- redirection of standard output for that command after which filename TMP_FOLDER_CONTENT.txt
will be substituted.
Also you can check returned value, as:
int errorcode = system("dir C:\* > "TMP_FILE_NAME);
printf("Command executed and returned a value %d\n", errorcode);
or taking into account command you use, change the logic of your program, e.g.:
int errorcode = system("dir C:\* > "TMP_FILE_NAME);
if( errorcode )
{
return errorcode;
}
UPDATE:
Alternatively, you could use pipes in C++, for example as shown in the answer to question How to execute a command and get output of command within C++ using POSIX?
Related Topics
Splitting a String by a Character
C++ Object Size with Virtual Methods
How to Create an Array When the Size Is a Variable Not a Constant
How to Add 2 Arbitrarily Sized Integers in C++
Why Floating Point Value Such as 3.14 Are Considered as Double by Default in Msvc
Efficient Thread-Safe Singleton in C++
Why Can't I Capture This By-Reference ('&This') in Lambda
How to Initialise a Dynamic Array in C++
Avoiding Copy of Objects with the "Return" Statement
How to Use a Mask to Iterate Files in a Directory with Boost
How to Get the CPU Usage Per Thread on Windows (Win32)
What Is the Proper Use of the Comma Operator
What Are the Distinctions Between the Various Symbols (*,&, etc) Combined with Parameters
Getdibits and Loop Through Pixels Using X, Y
Should I Return Exit_Success or 0 from Main()
How Does the Custom Deleter of Std::Unique_Ptr Work
Are Static Variables in a Base Class Shared by All Derived Classes