Если вы напишите операторы потоков, это сделает жизнь намного проще.
Это не так, как будто запись в двоичном формате значительно быстрее (поскольку вам нужно написать код для преобразования в различные форматы байтов), и в настоящее время пространство практически не имеет значения.
struct A
{
int day;
int mouth;
int year;
A(const int day, const int month, const int year)
{
// some simple arguments chech
if ( !(day >= 1 && day <= 31) || !(month >=1 && month <=12) || !(year <= 2010) )
throw std::exception();
this->day = day;
this->month = month;
this->year = year;
}
};
std::ostream& operator<<(std::ostream& str, A const& data)
{
return str << data.day << " " << data.month << " " << data.year << " ";
}
std::istream& operator>>(std::istream& str,A& data)
{
return str >> data.day >> data.month >> data.year;
}
С этим определением становится доступным и простым в использовании множество стандартных алгоритмов.
int main()
{
std::vector<A> allDates;
// Fill allDates with some dates.
// copy dates from a file:
std::ifstream history("plop");
std::copy(std::istream_iterator<A>(history),
std::istream_iterator<A>(),
std::back_inserter(allDates)
);
// Now save a set of dates to a file:
std::ofstream history("plop2");
std::copy(allDates.begin(),
allDates.end(),
std::ostream_iterator<A>(history)
);
}