Thread: gnome-terminal

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    61

    gnome-terminal

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(int argc, char const *argv[]) {
        
        system("gnome-terminal");
        
        printf("Hello World!");
    
    
        return 0;
    }
    Base on the code above I'm trying to call a new terminal, and do a printf their, but the output is showing on the old terminal where I compile it.

    How can I make it output on the new terminal??

    Thanks

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I hope this is very naive of me to say but I'm not sure you can. Or at least, there's no real simple or pretty way of doing this, is there?

    Can you pipe the output or something like that?

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    When you google "C program call new terminal". Stackflow comes up with this:

    The header file is stdlib.h and the function signature is int system(const char *command). So in your case you could call the function like this to spawn a new terminal window:
    Code:
    #include <stdlib.h>
    
    int main(void) {
        int exit_status = system("gnome-terminal");
    }

    In C it's common to check the return value of most function calls to determine if something went wrong or to get some more information about the call. The system() call returns the exit status of the command run, and is here stored in exit_status for further inspection.

  4. #4
    Registered User
    Join Date
    Jan 2014
    Posts
    45
    There are a few ways to go about it, probably the simplest is to write to a file from your program, and read from the file with gnome-terminal. The first thing to do is figure out how to run a program inside of gnome-terminal by specifying the program from the command-line. Most terminal emulators use the -e flag for this, so that will be my assumption.

    The next thing is to find (or write) a program that will read from a file. The obvious choice would be cat, but there is a problem with that. cat will read until it reaches the end of file, and when it does, it will terminate. We want a program that will continue to read the file, even when it has read to the end. 'tail -f' is a suitable program for this.

    This gives us the following command-line.

    Code:
    gnome-terminal -e "tail -f Filename"
    Before invoking this command from our program, we need to ensure that it won't cause the call to system to block, so we can proceed to write to the file afterward. I think gnome-terminal forks itself to the background automatically. For terminals like xterm, you'll need to ask the shell not to wait for xterm to finish by appending the & metacharacter to the command-line.

    With this in mind, it should be fairly straightforward to go ahead and write the program.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
            FILE *fout;  
            int c;
    
            if ((fout = fopen("output.txt", "w")) == NULL) {
                    fprintf(stderr, "unable to open output.txt (mode=\"w\")\n");
                    return EXIT_FAILURE;
            }
            system("gnome-terminal -e \"tail -f output.txt\"");
            while ((c = getchar()) != EOF) 
                    putc(c, fout); 
            fclose(fout);
            return 0;
    }
    One thing you might observe, after running this program, is that the lines don't show up in the terminal until 1) you enter a significant amount of text, or 2) you close the input stream. The reason this occurs is because stdio streams, that are associated with files, tend to be fully buffered. You can change the stream's buffering rule to line buffering by making the call "setvbuf(fout, NULL, _IOLBF, BUFSIZ)" immediately after the call to fopen succeeds.

    I think the next thing to think about is whether it's a suitable solution, or whether your problem would be better solved with a shell script. For most purposes I would lean toward the latter, although there were a few (admittedly rare) cases where I've needed to use something like this in C.
    Last edited by zyxwvuts; 10-01-2014 at 05:34 AM.

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    61
    Thanks, @zyxwvuts for the answer.
    It's not exactly want I but, your answer help to solve my problem. Thanks

  6. #6
    Registered User
    Join Date
    Jan 2014
    Posts
    45
    You're welcome. :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 08-28-2011, 09:01 PM
  2. Console, Terminal and Terminal Emulator
    By lehe in forum C Programming
    Replies: 4
    Last Post: 02-15-2009, 09:59 PM
  3. Gnome vs. KDE
    By LogicError in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-04-2005, 05:21 AM
  4. How to go to the GNOME
    By jacktibet in forum Linux Programming
    Replies: 3
    Last Post: 07-28-2003, 01:48 AM
  5. Gnome
    By Unregistered in forum Linux Programming
    Replies: 8
    Last Post: 07-08-2002, 09:51 PM