On 03/17/12 11:55, vectrum wrote:
> Hello,
> 
> I am learning c++ and planning to learn fltk programming as it has been
> hailed as one of the fine libs to program with but as my goal is to
> learn unix programming so I'm not sure how fltk manages system call.
> I want to display output of a simple fork call, how fltk manages it?
> 
> #include <unistd.h>
> int main()
> {
> pid_t pid;
> const char *name;
> pid = fork();
> if (pid == 0)
> {
> name = "I am the child";
> write(1, name, 15);
> write(1, "\n", 2);
> }
> else
> {
> name = "I am the parent";
> write(1, name, 16);
> write(1, "\n", 2);
> }
> return 0;
> }
> 
> I want to display the output of this code on a fltk based label or
> text box. Is it possible?
> Thank you.

        Sounds like you want to redirect stdin/out of your app
        to a pipe that you can then read, and stick it into
        an FLTK window.

        It's harder to handle the parent's output, since
        this would imply threads.

        But it's easy to get the child's output.

        An easy example is to rewrite the program using popen(),
        which does the fork() and redirection to a pipe() for you,
        and show the child's output in a dialog window:

#include <stdio.h>
#include <string>
#include <FL/Fl.H>
#include <FL/fl_ask.H>
#ifdef _WIN32
#define popen  _popen
#define pclose _pclose
#endif
int main(int argc, const char *argv[]) {
    if ( argc >= 2 ) {
        // CHILD
        const char *name = "I am the child\n";
        write(1, name, strlen(name));
        return(0);
    }
    std::string command;
    command = argv[0];
    command += " -child";
    std::string msg;
    FILE *fp = popen(command.c_str(), "r");
    if ( fp == 0 ) {
        msg += "Failed to execute: '";
        msg += command;
        msg += "'\n";
    } else {
        char s[1024];
        while ( fgets(s, sizeof(s)-1, fp) ) {
            msg += s;
        }
        pclose(fp);
    }
    fl_alert(msg.c_str());
    return(0);
}

        Note that only 3 lines of that code involve FLTK.

        Here's a more complex example showing how to use
        fltk with popen() handling both stdin and stdout.
        http://seriss.com/people/erco/fltk/#SimpleTerminal

        ..and an example without using popen() that uses
        raw fork():
        http://seriss.com/people/erco/fltk/#Fltk-tty
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to