0

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.

2
Contributors
3
Replies
5
Views
5 Years
Discussion Span
Last Post by TheNNS
0

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";

Edited by TheNNS: n/a

1

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.