Question

I have a program that puts some strings and integers in a structure. now I want to make it so if the program is opened it gets all information from the file and if the program is closed I want to save all info to the same file so I can find it the next time.

What is the best way to do that with one file to put it all in or a seperate file per each variable?

If I have figured that out I need to know how to find a specific line and than read the info from there. (like the name of line 59 goes to the 59th place in a structure array.), then I have to overwrite certain info like the amount of games played and how many of them are won, lost or tied. (it is a little game.)

Here is the structure:

struct Recgame{
    char name[20];
    char surname[20];
    int games;        //needs to be overwritable in file
    int won;          //needs to be overwritable in file
    int same;         //needs to be overwritable in file
    int lost;         //needs to be overwritable in file
    int place;        //needs to be overwritable in file
            int money;        //needs to be overwritable in file
} info[100];
Was it helpful?

Solution

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;

OTHER TIPS

You can use fwrite function in C to write the structure in a binary file, and fread to read it back again.

If you want to use C++ style file I/O, then you need to overload << and >> operator.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top