Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6210719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:12:35+00:00 2026-05-24T06:12:35+00:00

I have written a program which writes a list of data to a ‘.dat’

  • 0

I have written a program which writes a list of data to a ‘.dat’ file with the intention of then plotting it separately using gnuplot. Is there a way of making my code plot it automatically? My output is of the form:

x-coord    analytic    approximation
x-coord    analytic    approximation
x-coord    analytic    approximation
x-coord    analytic    approximation
x-coord    analytic    approximation
 ....

Ideally, when I run the code the graph would also be printed with an x-label, y-label and title (which could be changed from my C code). Many thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:12:37+00:00Added an answer on May 24, 2026 at 6:12 am

    I came across this while searching for something else regarding gnuplot. Even though it’s an old question, I thought I’d contribute some sample code. I use this for a program of mine, and I think it does a pretty tidy job. AFAIK, this PIPEing only works on Unix systems (see the edit below for Windows users). My gnuplot installation is the default install from the Ubuntu repository.

    #include <stdlib.h>
    #include <stdio.h>
    #define NUM_POINTS 5
    #define NUM_COMMANDS 2
    
    int main()
    {
        char * commandsForGnuplot[] = {"set title \"TITLEEEEE\"", "plot 'data.temp'"};
        double xvals[NUM_POINTS] = {1.0, 2.0, 3.0, 4.0, 5.0};
        double yvals[NUM_POINTS] = {5.0 ,3.0, 1.0, 3.0, 5.0};
        FILE * temp = fopen("data.temp", "w");
        /*Opens an interface that one can use to send commands as if they were typing into the
         *     gnuplot command line.  "The -persistent" keeps the plot open even after your
         *     C program terminates.
         */
        FILE * gnuplotPipe = popen ("gnuplot -persistent", "w");
        int i;
        for (i=0; i < NUM_POINTS; i++)
        {
        fprintf(temp, "%lf %lf \n", xvals[i], yvals[i]); //Write the data to a temporary file
        }
    
        for (i=0; i < NUM_COMMANDS; i++)
        {
        fprintf(gnuplotPipe, "%s \n", commandsForGnuplot[i]); //Send commands to gnuplot one by one.
        }
        return 0;
    }
    

    EDIT

    In my application, I also ran into the problem that the plot doesn’t appear until the calling program is closed. To get around this, add a fflush(gnuplotPipe) after you’ve used fprintf to send it your final command.

    I’ve also seen that Windows users may use _popen in place of popen — however I can’t confirm this as I don’t have Windows installed.

    EDIT 2

    One can avoid having to write to a file by sending gnuplot the plot '-' command followed by data points followed by the letter “e”.

    e.g.

    fprintf(gnuplotPipe, "plot '-' \n");
    int i;
    
    for (int i = 0; i < NUM_POINTS; i++)
    {
      fprintf(gnuplotPipe, "%lf %lf\n", xvals[i], yvals[i]);
    }
    
    fprintf(gnuplotPipe, "e");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program (written in C#) that reads/writes its data directly (direct file
I have a program, which writes data to a text file in the following
I have written this program, which sorts some ints using a functor: #include<iostream> #include<list>
I have a program written in VB.NET which stops a service that uses file
I have a program which i have myself written in java, but I want
I am beginning to port a program which is written in C and have
I have written a program in C to parse large XML files and then
I have a program which writes its output using ofstream. Everything works perfectly fine
I have written a program a.exe which launches another program I wrote, b.exe ,
I have written a program in Java to read in a text file of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.