I have a 2D array with generated doubles. I need to write this array to a CSV file so that I can open it in Excel. Is there a method or function in C++ that allows for this to be done? I could do it manually, but it would take a lot longer. Any help is appreciated, thanks.

Ok, I feel like an idiot now. This is way easier to do than it sounds. I just used two for loops and that did the trick. I just need to get rid of the commas at the end and I should good. Here's the code I used.

if (myfile.is_open())
	  {
		  for (int k = 0; k < 20; k++)
		  {
			  for (int i = 0; i < 20; i++)
			  {
				  myfile << temp[0+k][0+i] << ",";
			  }
			  myfile << endl;
		  }
	  }
	  else cout << "Unable to open file";

Traverse the array and write that in a text file with extension .csv

Write your 2d array entries in comma separated form ending with newline character.
for example

cout<<TestArray[0][0]<<","<<TestArray[0][1]<<newline;
cout<<TestArray[0][0]<<","<<TestArray[0][1]<<newline;

I have provided just the format for you, for above task you can iterate complete array and write into a file with extension .csv.
It will solve your problem.

thanks for the tajendra, it works great.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.