Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++, Gnuplot: Create temporary file in RAM and use it in Gnuplot

Tags:

c++

gnuplot

I am a beginner in C++. Suppose I have a C++ program that outputs a function, for example f(x)=x*x, and I need it evaluated and printed. This can be easily done within the code.

Is it possible to create a temporary file with the result of the evaluation in RAM? As in, it would be an std::vector, most likely, so it would reside in the heap (no?).

Then, would it be possible to use system() (or anything from within the code) to call gnuplot with the file from the memory?

And, as a final whim, can these two be done without an external virtual RAM disk?

like image 650
a concerned citizen Avatar asked May 13 '26 05:05

a concerned citizen


1 Answers

So in Linux, using pipes, you could do the following as a first example:

cat data.txt | gnuplot -p -e "plot '-' w l"

where data.txt is a two column file. Instead of that file you can have your program produce some data and you could add an extra setting for gnuplot to make it slightly more involved:

./program | gnuplot -p -e "set ylabel 'E'; plot '-' w l"

Now this is all you need. Note that the -p = persistent, -e = interactive(?) and the semicolons (;) between the commands are required. Something similar might work in windows. I hope this helpts.

Extra stuff: If you have a lot of settings you can put them in a file, ex:

$ cat settings.txt
set logs;
set ylabel "hejsan";
set xlabel "energy";
plot '-' w l

and then:

./program | gnuplot -p -e "$(<settings.txt)"

Dont miss any symbols in "$(<filename)"! This might only apply to the Bash shell in Linux though. You can try the cygwin environment. Or just install ubuntu.

EDIT a week later

In case you want to do this to save your SSD, it of course wont hurt, but probably doesn't matter. The SSD's have intelligent management so it wont write to the same physical segment every time. The file would "move around", and they are built to last for hundreds of terabytes worth of cycling.

like image 143
Jonatan Öström Avatar answered May 14 '26 19:05

Jonatan Öström



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!