ChipotleHS ChipotleHS - 2 months ago 9
C Question

Gnuplot and C "x range is invalid"

I'm trying to generate a data file and to plot it with Gnuplot. The problem is when I keep my Nstep lower than 348 I get the error


line 0: warning: Skipping data file with no valid points

plot 'plot.txt' using 1:2 with lines
^
line 0: x range is invalid



But I keep the
Nstep
higher than 348 everything works fine. I do not understand why. Here is my C code:

int main(void){

int Nstep = 348;

//omitted part...

FILE *pipe = fopen("plot.txt", "w+");

while (n<Nstep) {
pos[n+1] = pos[n] + v[n]*h;
v[n+1] = v[n] + h * Fx(pos[n]);

fprintf(pipe, "%d %05.3lf\n", n, v[n]);
n++;
}

close(pipe);

system("gnuplot -p -e \"plot 'plot.txt' using 1:2 with lines\"");

return 0;
}


plot.txt example (Nstep = 10)

1 100.000
2 99.000
3 97.000
4 94.010
5 90.050
6 85.150
7 79.349
8 72.697
9 65.252
10 57.079

Answer

I am unable to replicate your error as you didn't include the full source (function Fx and definitions of pos and v). You are calling the wrong close. You should call fclose() (this will flush the file handle too).

fclose(pipe)

And not

close(pipe)

You could explicitly flush the data by calling fflush().