save data in cvs file

How can i save the output of c++ program in cvs file?

I have to this type of data

I have a loop in that i have data of three types and i want to asve it on
cvs file format. Saved data should be some thing like this

id name place gender
1 rita abc female
2 Ali def male

based on loop iteration, data will be iterated and saved in cvs file format.
How can i do that in c++?
Kindly let me know .
thanks
Just output a comma as the separator between each value. At the end of each line, instead of a comma, use an ordinary newline or endl.
Hi ,

i tested the below program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include<string>
#include <iomanip>
using namespace std;
int main()
{
	ifstream indata;
	ofstream outdata;
	outdata.open("aaron.csv", ios::app);
	outdata << "a1,b1,c1" << endl;
	outdata << "a2,b2,c2" << endl;

	indata.open("aaron.csv");
	string cell1;

	indata >> cell1;

	cout << cell1 << "\n\n";

	system("pause");
	return 0;
}


When i run the program every time. i found previously runned data with my newly saved data.
I want to update the saved data. don't want to maintain previous data. how can fix it . thanks
Well, you opened the file in append mode, with the ios::app option.
That's why it appends the new data to the end of the existing file.
Topic archived. No new replies allowed.