Plot a file.txt with Gnuplot?

Hello to all!I need to write on a file.txt values (number of iterations of some sorting algorithms) using c++ fwrite, the values will represent the y axis and the array sizes the x axis, I have no experience in using fwrite, and because I need to write the two columns in a certain syntaxe, I need someone to explain to me how to do it? please don't ignore this
the file.x should be like this:

#arrSize Values
10 3
50 7
100 19
200 25


(just random numbers)
Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/reference/cstdio/fwrite/
thanks a lot!
I can't see why fwrite is helpful here. Successive items are of two different species, so they aren't likely to be in the same array. A simple example using normal file operations is below. Apart from opening and closing the file it writes just like cout would.

Gnuplot isn't that fussy. You don't strictly need a header (anything after # is ignored) and your columns of data are fine with just spaces between; they don't need perfect alignment.

Plot in gnuplot with
p 'file.txt' u 1:2 w l



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<fstream>                                      // required for file operations
using namespace std;


int main()
{
   // Generate your data - I'll have to fake that here and guess that they are ints
   int num = 4;                                        // number of data items
   int x[] = { 10, 50, 100, 200 };
   int y[] = {  3,  7,  19,  25 };

   // Write to file
   ofstream fileOut( "file.txt" );                     // open file
   
   fileOut << "#arrSize Values" << endl;               // write a header (not strictly necessary for gnuplot)
   
   for ( int i = 0; i < num; i++ )                     // loop round, dealing with each line of data
   {
      fileOut << x[i] << "  " << y[i] << endl;         // gnuplot happy with space between items; remember newline
   }
   
   fileOut.close();                                    // finished with file; close it
}
Last edited on
thanks a lot for your help i wish i've seen it earlier, project delieverd! i used ofstream file;
then everytime i call a sorting function with 'nbrOfOperation++' in it, i do this:
file<<arraysize1<<" "<<nbrOfOperations<<endl;
Topic archived. No new replies allowed.