سؤال

وأحتاج إلى تشغيل أمر لينكس CLI والحصول على إخراج المعياري لها من C.

ويمكنني استخدام الأنابيب () لإنشاء الأنابيب، ثم الشوكة / إكسيك، وإعادة توجيه اصف المعياري الطفل في الأنابيب قبل استدعاء إكسيك ()، والقراءة من الأنابيب في الأصل. بالاضافة الى انني سوف تحتاج إلى الانتظار على الطفل.

هل هناك دعوة بسيطة للقيام شوكة + توجيه + إكسيك + الانتظار، مثل نظام () لا شوكة + إكسيك + الانتظار، النظام فقط () لا تفعل إعادة التوجيه.

ولقد popen هناك ()، والتي لا شوكة + توجيه + إكسيك، ولكنه لا يفعل الانتظار، لذلك لا أستطيع الحصول على حالة خروج.

هل كانت مفيدة؟

المحلول

هل هذا فقط؟

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().

نصائح أخرى

والخصلة لديه وظيفة لطيفة لهذا - g_spawn_sync(): HTTP: //library.gnome. غزاله / جمعة / سطحي / مستقرة / سطحي-التفريخ-Processes.html # ز تفرخ المزامنة

وعلى سبيل المثال، لتشغيل أمر والحصول على حالة خروج والإخراج:

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
}

استخدم popen() وpclose().


وpopen() لا ينتظر في الواقع، وبطبيعة الحال، ولكن يقرأ على الأنابيب ومنع حتى يكون هناك بيانات متوفرة.

وينتظر pclose()، ولكن اصفا إياه قبل الأوان يمكن أن تقطع بعض الناتج من عملية متشعب. فأنت تريد لتحديد من تيار عندما يتم الطفل ...


وربما سبق في <لأ href = "https://stackoverflow.com/questions/43116/how-can-i-run-an-external-program-from-c-and-parse-its-output" > href="https://stackoverflow.com/questions/43116/how-can-i-run-an-external-program-from-c-and-parse-its-output"> كيف يمكنني تشغيل برنامج خارجي من C وتحليل انتاجها؟

وهنا هو ما يمكنني استخدام:

   /* 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);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top