// A program to write to a file, which can be opened in a spreadsheet #include <cstdlib> #include <fstream> #include <iostream> int main(int argc, char* argv[]) { // open up a file stream to write data std::ofstream output; // here we are going to use comma separated variables, so end the filename with .csv // If I am on a normal windows desktop the open command will be something like: output.open("C:/Documents and Settings/Paul Johnson/My Documents/Data/test.csv"); // If I am on a university cluster windows machine the open command will be something like: // output.open("P:/Data/test.csv"); // If I am on a unix machine the open command will be something like: // output.open("/home/pjohnson/Data/test.csv"); if(!output.is_open()) { // NOTE!!!! The file will not open unless the directory exists!!! std::cout << " File not opened \n"; // stop the program here throw; } // write x vs x^2 to a file for(int i=0;i<11;i++) { double x=i*0.1; output << x << " , " << x*x << std::endl; } // file write successful then close file std::cout << " File write successful \n"; output.close(); // on visual studio you need to pause terminal system("pause"); return 0; }