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.