0

I need some help.

I have two arrays (x_val[] and y_val[]) of data with the x and y values of some points in the plane, so that (x,y) is a point in the 2D space. I write the data following the next format:

data_file.dat

Then I use GNUPLOT for drawing all the points:

plot 'data_file.dat' u 1:2 w points

The problem is that the size of the file is almost 1Gb and I need to generate almost 500 files. So I thought that it could be better to write the data in binary format and then plot it, but I don't know how to do either.

I would like to do it without changing the arrays x_val[] and y_val[] please.

Thank you.

CAMILO HG
  • 183
  • 1
  • 9
  • This is a tall order.... Have a look at fopen. For example fopen( "data_file.data", "wb"); – atomSmasher Jul 18 '17 at 22:07
  • I tried to do it using fopen( "data_file.data", "wb"); but I got the same result: a file with a ASCII like format ... Same format, same size – CAMILO HG Jul 18 '17 at 22:18
  • Ok, so you opened in binary mode with `"wb"` but how did you write the data? What kind of write statement did you use? Or did you use `printf` and end up writing it as a string? Use `open`, `read` and `write`, not `printf` and `scanf` which are for *formatted* data. – lurker Jul 18 '17 at 22:20
  • How to use `gnuplot` is a completely separate question. Here's a complementary link to the [`gnuplot` documentation](http://www.gnuplot.info/docs_5.0/gnuplot.pdf). – lurker Jul 18 '17 at 22:23
  • I tried using fprinf("%lf \t %lf \n", x[ i ] , y[ i ] ) ... Because I saw fwrite() receibes just 1 pointer, but I have 2: x_val[ ] and y_val[ ] – CAMILO HG Jul 18 '17 at 22:28
  • Yeah, like I said, `fprintf` such as the one you did is going to format in ASCII, leaving you where you were before. You need `fwrite`. You're writing in binary. You do only get one pointer. So the simplest thing to do is write out `x[i]` and `y[i]` with separate `fwrite` calls. There's no rule that you need to use just one call to write out both coordinates. `fwrite(&x[i], 1, sizeof int, fp);` and `fwrite(&y[i], 1, sizeof int, fp);`. – lurker Jul 18 '17 at 22:30
  • Ok, I think I got it. But how do I know it has two columns ? Or how do I plot it ? – CAMILO HG Jul 18 '17 at 22:32
  • You already know the data has two columns. Does your program have to figure out automatically that there are two coordinates? Are you supporting cases where there are more or less than 2 coordinates? To your second question, see the GNU plot documentation. Read more specifically, for example, [Gnuplot: binary general](http://gnuplot.sourceforge.net/docs_4.2/node103.html) – lurker Jul 18 '17 at 22:34
  • Ok. Thank you everybody! – CAMILO HG Jul 18 '17 at 22:36

0 Answers0