Gnome GTK / Spawn CLI app - read output

This is a discussion on Gnome GTK / Spawn CLI app - read output within the Linux Programming forums, part of the Platform Specific Boards category; Could someone point me to the source code or an example program that shows how to spawn a new application ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    2

    Gnome GTK / Spawn CLI app - read output

    Could someone point me to the source code or an example program that shows how to spawn a new application from gnome/gtk to a CLI application and read its output back into your gnome/gtk application?

    Example:
    GTK application calls "ls -a" and then the output of ls is shown in a GTK_ENTRY widget.

    Thank you very much,

    Matthew Ozor

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I would do this by forking the command and writing it's output to a tmp file, then reading the tmp file into the entry.

    Code:
    ls > tmp.txt
    I promise you will not find a way to feed a Command Line Interface application DIRECTLY into a gtk program, or for that matter any C program (or for that matter any program in any language).

    Which also means you can't store the output in a string, etc. You have to use a text file.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    I promise you will not find a way to feed a Command Line Interface application DIRECTLY into a gtk program, or for that matter any C program (or for that matter any program in any language).
    Umm . . . what about popen()?

    (It lets you essentially start a new process and pipe that input into your program. I guess it technically uses a file stream as well, but without the hassle of a separate file.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    promises promises

    Umm . . . what about popen()?

    Yes indeed! Well, I guess we are all living in a better universe for it anyway. This may be why they call me "the boy who..."
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Besides popen(), there's of course the more powerful (but more tiresome) pipe/fork/exec dance.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    Question

    Quote Originally Posted by CornedBee View Post
    Besides popen(), there's of course the more powerful (but more tiresome) pipe/fork/exec dance.
    What I thought was the ONLY way, actually. So before I become a popen() convert, what does Corned Bee mean by "more powerful"?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    popen() gives you a stream through which you can either read the child's stdout or write to its stdin. With the fork dance, you can pipe both input and output. For controlling an interactive shell process from within a GUI, this is actually essential.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    2

    Thumbs up

    Thank you very much for suggesting the popen() function as this is a nice clean way to add a external programs output to a gtk+ textview widget. I do intend to learn how to use pipes as this would give a real time output.

    Here is a example code for that maybe helpful for people learning. It does require a pointer to a real textview1 widget to work.


    Code:
      FILE *fp;
      
      GtkTextBuffer *buffer;
      GtkTextIter   iter; 
    
      buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (textview1));
    
      fp = popen("ls -a", "r");			
    
      while ( fgets( line, sizeof line, fp))
      {
        gtk_text_buffer_get_end_iter (buffer, &iter); 		       
        gtk_text_buffer_insert (buffer, &iter, line, -1); 
      }
    
     pclose(fp);
    Thanks again,
    Matthew Ozor

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  2. I am lost on how to read from file and output to file?
    By vicvic2477 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2005, 10:52 AM
  3. No Output? Can someone proof read this please?
    By GrlNewB in forum C++ Programming
    Replies: 4
    Last Post: 07-06-2003, 09:36 PM
  4. Replies: 1
    Last Post: 12-31-2001, 04:04 PM
  5. To all who read last post formatted output
    By spliff in forum C Programming
    Replies: 8
    Last Post: 08-21-2001, 03:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21