Here I have a structure called contacts
typedef struct contacts
{
string name; //{jhonathan , anderson , felicia}
string nickName; //{jhonny , andy , felic}
string phoneNumber; // {13453514 ,148039 , 328490}
string carrier; // {atandt , coolmobiles , atandt }
string address; // {1bcd , gfhs ,jhtd }
} contactDetails;
vector <contactDetails> proContactFile;
I'm trying to write the data inside my vector to a output file.For this i've written the following code
ofstream output_file("temp.csv");
ostream_iterator<contactDetails> output_iterator(output_file, "\n");
copy(begin(proContactFile),end(proContactFile), output_iterator);
But this code always gives me an error.Also I want to write the data to the file with the following way.
Name,Nick name,Phone number,Carrier,Address
Whats wrong with my code?
std::ostream_iterator<T>
invokesoperator<<
for the typeT
. You need to write code forstd::ostream& operator<<(std::ostream& os, const contactDetails& cont)
so thatostream_iterator
iterates and write to its output stream.