The C++ way to do this, is writing an stream-inserter and a stream-extractor for the struct Recgame. The prototypes are:
std::ostream& operator<<( std::ostream& out, const Recgame& recgame );
std::istream& operator>>( std::istream& in, Recgame& recgame );
After this, You can easy write the infos to a file
ofstream file("afile.txt");
for( int i=0; i<n; ++i ) // n should be the number of the objects
file << info[i];
the implementation of the writing could be:
std::ostream& operator<<( std::ostream& out, const Recgame& recgame )
{
// make sure, that the char-arrays contain a closing char(0) -> ends
out << recgame.name << "\n";
out << recgame.surname << "\n";
out << recgame.games << " " << recgame.won << " " << recgame.same << " " <<
recgame.lost << " " << recgame.place << " " << recgame.money << "\n";
return out;
}
the implementation of the reading extractor
std::istream& operator>>( std::istream& in, Recgame& recgame )
{
in >> std::skipws; // skip leading spaces
in.getline( recgame.name, 20 ).ignore( std::numeric_limits< std::streamsize >::max(), '\n' ); // requires #include <limits>
in.getline( recgame.surname, 20 ).ignore( std::numeric_limits< std::streamsize >::max(), '\n' );
in >> recgame.games >> recgame.won >> recgame.same >>
recgame.lost >> recgame.place >> recgame.money;
return in;
}
read it from file:
ifstream file("afile.txt");
int n = 0; // number of read objects
for( ; n < N && file >> info[n]; ++n ) // -> const int N = 100;
;
if( file.eof() )
cout << "Ok - read until end of file\n";
cout << "read " << n << " objects" << endl;