Frage

Ich brauche einen Linux-CLI-Befehl auszuführen und seine stdout Ausgabe von C zu erhalten.

kann ich Rohr () ein Rohr zu schaffen, dann fork / exec, Kinder stdout Descriptor in das Rohr umgeleitet, bevor exec () aufgerufen wird, und aus dem Rohr in Eltern zu lesen. Außerdem werde ich auf das Kind warten.

Gibt es eine einfache Anruf Gabel zu tun + + exec umleiten + warten, wie system () tut fork + exec + warten, nur system () ist die Umleitung nicht.

Es gibt popen (), die + nicht fork + exec umleiten, aber wartet nicht tun, so kann ich nicht Exit-Status erhalten.

War es hilfreich?

Lösung

Ist es das?

NAME
       popen, pclose - process I/O

SYNOPSIS
       #include <stdio.h>  

       FILE *popen(const char *command, const char *type);

       int pclose(FILE *stream);

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 
containing 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 be 
either ‘r’ for reading or ‘w’ for writing.

       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().  
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 command’s standard output, and the 
command’s standard input is the same as that of the process that called 
popen().

       Note that output popen() streams are fully buffered by default.

       The pclose() function waits for the associated process to terminate 
and returns the exit status of the command as returned by wait4().

Andere Tipps

GLib hat eine nette Funktion für diesen - g_spawn_sync(): http: //library.gnome. org / devel / glib / stable / glib-Spawning-Processes.html # g-Spawn-sync

Zum Beispiel, um einen Befehl auszuführen und erhalten ihren Exit-Status und Ausgang:

const char *argv[] = { "your_command", NULL };
char *output = NULL; // will contain command output
GError *error = NULL;
int exit_status = 0;
if (!g_spawn_sync(NULL, argv, NULL, 0, NULL, NULL, 
                  &output, NULL, &exit_status, &error))
{
  // handle error here
}

Mit popen() und pclose().


popen() nicht wirklich warten, natürlich, aber liest auf dem Rohr blockiert, bis Daten verfügbar sind.

pclose() wartet, aber es ruft vorzeitig könnte eine Ausgabe aus dem gegabelten Prozess abgeschnitten. Sie wollen aus dem Stream, um zu bestimmen, wenn das Kind getan wird ...


Möglicherweise bereits diskutiert unter Wie kann ich ein externes Programm von C ausführen und seine Ausgabe analysieren?

Hier ist, was ich benutze:

   /* simply invoke a app, pipe output*/
    pipe = popen(buf, "r" );
    if (pipe == NULL ) {
        printf("invoking %s failed: %s\n", buf, strerror(errno));
        return 1;
    }

    waitfor(10);

    while(!feof(pipe) ) {
        if( fgets( buf, 128, pipe ) != NULL ) {
            printf("%s\n", buf );
        }
    }

    /* Close pipe */
    rc = pclose(pipe);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top