For output, simply use the ostream
interface (which works with cin
or fstream
or...)
std::ostream& operator<<(std::ostream& stream, const Reservation& reservation) {
stream
<< reservation.ID << ","
<< reservation.name << ","
//... and so on
}
For parsing the input, if you can assume that the fields will always be separated by a single ','
(no spaces), and your strings (like name) won't contain commas or spaces, you can do:
std::istream& operator>>(std::istream& stream, Reservation& reservation) {
stream >> reservation.ID;
stream::char_type ignored;
stream >> ignored; // skip over ','
std::string aString;
stream >> aString; // read name
// Now copy to char array. To make sure we don't overflow it,
// cut string to MAX_NAME_SIZE, and leave additional space for the
// terminating '\0'.
aString = aString.substr(0, MAX_NAME_SIZE-1);
strcpy(reservation.name, aString.c_str());
// and so on..
}
But this is a very basic solution. It will suffice if your input format is guaranteed to fulfill my assumptions noted above, and if you absolutely don't want to use 3rd party code. But in general, I'd advise you to find a library to read & write CSV data, or similar. It makes handling details like quoted strings, additional whitespace etc. much simpler.
And you still need to create the link relationships between your Reservation
s.