Thread: More *nix pipe blues.. I can't get gnuplot to display the plot.

  1. #1
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329

    More *nix pipe blues.. I can't get gnuplot to display the plot.

    I can't get gnuplot to display the plot of sin(5). Here is what I've attempted...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      FILE *command;
    
      if ((command = popen("gnuplot", "w")) == NULL) {
        (void)fprintf(stderr, "pipe error\n");
        exit(1);
      }
    
      if (setvbuf(command, NULL, _IONBF, 0) != 0) {
        (void)fprintf(stderr, "setbuf error\n");
        exit(1);
      }
    
      (void)fprintf(command, "plot \"%s\" ", "sin(5)");
      /*fflush(command);*/
    
      pclose(command);
      exit(0);
    }
    But when I compile and run it, the program just exits. Below is what I get

    $ gcc -Wall -Wextra dd2.c -o dd2
    $ ./dd2
    $ echo $?
    0
    $

  2. #2
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Never mind. I figured it out. For whatever reasons, I needed a newline in the following line code..

    Code:
    (void)fprintf(command, "plot \"%s\" ", "sin(5)");
    So I changed the code to the following.

    Code:
    (void)fprintf(command, "%s\n", "plot sin(5)");
    Also, I needed to have the follow so the plotter wouldn't exit right away..

    Code:
    while ((c = getchar()) != EOF)
        ;
    Anyways, here is the complete solution..

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      FILE *command;
      int c;
    
      if ((command = popen("gnuplot", "w")) == NULL) {
        (void)fprintf(stderr, "pipe error\n");
        exit(1);
      }
    
      if (setvbuf(command, NULL, _IONBF, 0) != 0) {
        (void)fprintf(stderr, "setbuf error\n");
        exit(1);
      }
    
      (void)fprintf(command, "%s\n", "plot sin(5)");
      fflush(command);
    
      while ((c = getchar()) != EOF)
        ;
    
      pclose(command);
      exit(0);
    }
    Now, I have one more question. Why does gnuplot exit right away when I don't include

    Code:
    while ((c = getchar()) != EOF)
        ;
    but something like ddd (the data display debugger) doesn't?
    Last edited by Overworked_PhD; 02-14-2010 at 10:04 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cont of IPC using PIPE
    By BMathis in forum C Programming
    Replies: 1
    Last Post: 03-15-2009, 05:16 PM
  2. Pipe class.
    By eXeCuTeR in forum Linux Programming
    Replies: 8
    Last Post: 08-21-2008, 03:44 AM
  3. Pipe(): Interprocess or Intraprocess comm?
    By @nthony in forum C Programming
    Replies: 2
    Last Post: 03-28-2007, 07:27 PM
  4. pipe to file and still display to screen?
    By chrispyone in forum Tech Board
    Replies: 9
    Last Post: 03-16-2005, 03:27 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM