А есть ли возможность записать в файл результат выполнения команды, например, ls?
теоретически можно выполнить команды с помощью system(3). На практике это ОЧЕНЬ плохая идея. Это вам не пхп, тут мешать не нужно. Пишите на C++, есть ведь способы прочитать каталог.
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
ofstream out("text", ios::out | ios::app);
out << system("ls");
out.close();
}
В результате запуска полученного бинарика, открылось окно xterm, в котором был перечень имеющихся в каталоге файлов, в созданном файле "text" имелся только 0 (ноль) и больше ничего.
$ man system
.................
RETURN VALUE
The value returned is -1 on error (e.g., fork(2) failed), and the
return status of the command otherwise. This latter return status is
in the format specified in wait(2). Thus, the exit code of the command
will be WEXITSTATUS(status). In case /bin/sh could not be executed,
the exit status will be that of a command that does exit(127).
If the value of command is NULL, system() returns nonzero if the shell
is available, and zero if not.
system() does not affect the wait status of any other children.
Если Вы хотите запивать вывод команды, то обратите внимание на popen.
man popen
...............
. FILE *popen(const char *command, const char *type);
................
DESCRIPTION
The popen() function opens a process by creating a pipe, forking, and
invoking the shell. Since a pipe is by definition unidirectional, the
type argument may specify only reading or writing, not both; the
resulting stream is correspondingly read-only or write-only.
The command argument is a pointer to a null-terminated string contain‐
ing a shell command line. This command is passed to /bin/sh using the
-c flag; interpretation, if any, is performed by the shell. The type
argument is a pointer to a null-terminated string which must contain
either the letter 'r' for reading or the letter 'w' for writing. Since
glibc 2.9, this argument can additionally include the letter 'e', which
causes the close-on-exec flag (FD_CLOEXEC) to be set on the underlying
file descriptor; see the description of the O_CLOEXEC flag in open(2)
for reasons why this may be useful.
The return value from popen() is a normal standard I/O stream in all
respects save that it must be closed with pclose() rather than
fclose(3). Writing to such a stream writes to the standard input of
the command; the command's standard output is the same as that of the
process that called popen(), unless this is altered by the command
itself. Conversely, reading from a "popened" stream reads the com‐
mand's standard output, and the command's standard input is the same as
that of the process that called popen().