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:
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.
#include<iostream>
#include<fstream> // required for file operations
usingnamespace 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
}
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;