Help Outputting Data into CSV from C++

Sep 28, 2011 at 2:24am
Hi guys,

I am having trouble getting my code to output the data into a CSV file. Here is the main function. Everything runs correctly and the main function works and gives the correct output in a terminal but it doesn't put the data into the csv file as I thought it should. Any help would be greatly appreciated. Thanks for taking a look.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int main()
{
	int no_of_disks;
	clock_t time_before, time_after;
	float diff1, diff2;

	ofstream output_file("Data.csv");
	
	for (no_of_disks = 3; no_of_disks <= 30; no_of_disks++)
	{
		time_before = clock();
		Move_Using_Four_Pegs (no_of_disks,no_of_disks, 'A' , 'B', 'D', 'C');
		time_after = clock();
		diff1 = ((float) time_after - (float) time_before);

		time_before = clock();
		Move_Using_Three_Pegs (no_of_disks, no_of_disks, 'A' , 'C', 'B');
		time_after = clock();
		diff2 = ((float) time_after - (float) time_before);

		cout << "%age change in running time for ";
		cout << no_of_disks << " disks, when there is an extra peg = ";
		cout << ((diff1 - diff2)/diff2) * 100 << endl;

		output_file << no_of_disks << ",";
		output_file << ((diff1 - diff2)/diff2)*100 << endl;
	}
}
Sep 28, 2011 at 2:41am
Perhaps I am missing something, but I see no error here.
Do you have write permission on the cwd?
Sep 28, 2011 at 2:45am
I have all permissions. It may just be that it is running correctly but I do not know where to access the data file that is created. I figured it would just pop up in excel which it does not so then I tried to look for it in excel as possibly being saved. I am probably just an idiot and not realizing that it is saved somewhere obvious that I am not looking but I'm not sure. I am very new to C++.
Sep 28, 2011 at 2:48am
It will probably be saved in the same place as your program. Also, it will not be saved in CSV format - you just gave it the CSV extension.
Sep 28, 2011 at 2:52am
Sweet!! I just figured it out. It was working the whole time, I just didn't realize where it was saving. Thank you both for your help!
Sep 28, 2011 at 3:31am
The file the example creates is CSV format:

3,1.2345
4,6.7890
5,3.1415
6,2.3571
...
Topic archived. No new replies allowed.