2014-06-17 1 views
0

function g_spawn_command_line_sync() 인수 "gchar **standard_output" 있습니다입심 - 2.0 : - g_spawn_command_line_sync() 알 수없는 표준 출력 길이

https://developer.gnome.org/glib/stable/glib-Spawning-Processes.html#g-spawn-command-line-sync

내가 standard_output에서 바이너리 데이터를 읽을 필요가 있지만, 나는하지 standard_output의 길이 알려져 있습니다.

http://fossies.org/dox/glib-2.38.2/gspawn-win32_8c_source.html#l01452

Function g_spawn_command_line_sync(): 
실행 :

GString *outstr = NULL; 
*standard_output = g_string_free (outstr, FALSE); 

Struct GString include "gsize len" 있지만 "gchar **" 접근 g_spawn_command_line_sync().

다음 해결책이 있습니다. 나는 사용하지 않는 size of stdoutstderr에 기록한다.

예제 코드 :

#include <stdio.h> 
#include <stdlib.h> 
#include <glib.h> 

int main() 
{ 
    gint exit_status = 0; 
    gchar *p_stdout = NULL; 
    gchar *p_stderr = NULL; 
    GError *p_error = NULL; 
    gboolean result; 

    result = g_spawn_command_line_sync("./make_image.py", &p_stdout, &p_stderr, &exit_status, &p_error); 

    if (!result) { 
     if (p_error != NULL) { 
      printf(p_error->message); 
     } 
     else { 
      printf("ERROR: Command not run\n"); 
     } 
    } 
    else if (exit_status != 0) { 
     printf(p_stderr); 
    } 
    else { 
     int size = atoi(p_stderr); 
     gchar *p_c = p_stdout; 

     for (int i = 0; i < size; ++i) { 
      fputc(*p_c++, stdout); 
     } 

     //printf(p_stdout); 
    } 

    if (p_stdout) { 
     g_free(p_stdout); 
    } 

    if (p_stderr) { 
     g_free(p_stderr); 
    } 

    if (p_error) { 
     g_error_free(p_error); 
    } 

    return 0; 
} 

답변

1

사용 g_spawn_async_with_pipes. 파일 설명자에서 2 진 데이터를 읽는 것은 쉽습니다. 자식이 언제 종료되는지 감지해야한다면 g_child_watch_add 또는 g_child_watch_add_full을 사용하여 콜백을 추가하십시오. 그러나 오류가 발생할 때까지는 설명자를 읽는 것만으로도 쉽게 벗어날 수 있습니다.

관련 문제