Troubles with binary

To Chervil. Errors between lines 6 and 24. I suppose you will correct them easily. Thank you very much.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*artillery.cpp*/
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
struct PlayerRecord
{
        int x;
        int y;
        int z;
        //PlayerRecord(int xx,int yy, int zz):x(i),y(j),z(k) {}
};
//PlayerRecord rec(rec.x, rec.y, rec.z);
//PlayerRecord in_rec ( in_rec.x, in_rec.y, in_rec.z);

PlayerRecord rec ( rec.x, rec.y, rec.z )
{
        fstream a_file( "recs.bin", ios::trunc | ios::binary | ios::in | ios::out );
        a_file.write(reinterpret_cast<char*>( & rec.x ), sizeof( rec.x ) );
        a_file.write(reinterpret_cast<char*>( & rec.y ), sizeof( rec.y ) );
        a_file.write(reinterpret_cast<char*>( & rec.z ), sizeof( rec.z ) );
}
PlayerRecord in_rec ( in_rec.x, in_rec.y, in_rec.z)
{
        a_file.seekg( 0, ios::beg );

        if ( ! a_file.read( reinterpret_cast<char*>( & in_rec.x ), sizeof( in_rec.x ) ) )
        {
                cout << "Error reading from file(1)" << endl;
                return 1;
        }
        if ( ! a_file.read( reinterpret_cast<char*>(& in_rec.y ), sizeof( in_rec.y ) ) )
        {
                cout << "Error reading from file(2)" << endl;
                return 1;
        }
        if ( ! a_file.read( reinterpret_cast<char*>(& in_rec.z ), sizeof( in_rec.z ) ) )
        {
                cout << "Error reading from file(3)" << endl;
                return 1;
        }
        cout <<x<<' '<<y<<' '<<z<<endl;
}

int main ()
{
        cout <<"writing\n";
        rec.x = 11;
        rec.y = 1234567890;
        rec.z = 567;
        PlayerRecord rec(rec.x, rec.y, rec.z);
        rec.x=10;
        rec.y=11;
        rec.z=12;
        PlayerRecord rec(rec.x, rec,y, rec.z);
        rec.x=56;
        rec.y=89;
        rec.z=432;
        PlayerRecord rec(rec.x, rec,y, rec.z);
        cout << "reading\n";
        PlayerRecord in_rec(rec.x,rec.y, rec.z) ;
        PlayerRecord in_rec(rec.x,rec.y, rec.z) ;
        PlayerRecord in_rec(rec.x,rec.y, rec.z) ;
}
return 0;
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
Well, I tried to fix the problems, but there were quite a number of them, so I ended up re-writing some parts of the code.

As much for my own convenience as anything, I created two separate programs, one for creating the file, and another for reading from the file. These could be combined, it was just a way of trying to keep things fairly simple.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <fstream>
#include <string>
#include <iostream>

using namespace std;

struct PlayerRecord
{
    int x;
    int y;
    int z;
    PlayerRecord(int xx,int yy, int zz) : x(xx), y(yy), z(zz) {}
};

void to_file ( fstream & outfile, const PlayerRecord & rec )
{
    outfile.write(reinterpret_cast<const char*>( & rec.x ), sizeof( rec.x ) );
    outfile.write(reinterpret_cast<const char*>( & rec.y ), sizeof( rec.y ) );
    outfile.write(reinterpret_cast<const char*>( & rec.z ), sizeof( rec.z ) );
}

int main ()
{
    cout <<"writing\n";

    fstream a_file( "recs.bin", ios::trunc | ios::binary | ios::in | ios::out );

    PlayerRecord rec(11, 1234567890, 567);
    to_file(a_file, rec);

    rec.x=10;
    rec.y=11;
    rec.z=12;
    to_file(a_file, rec);

    to_file(a_file, PlayerRecord(56, 89, 432));   
}


Above, I used a variety of code in main(), not for any particular reason.

Now to read the file and display the contents:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <fstream>
#include <string>
#include <iostream>

using namespace std;

struct PlayerRecord
{
    int x;
    int y;
    int z;
    PlayerRecord(int xx,int yy, int zz) : x(xx), y(yy), z(zz) {}
    PlayerRecord() : x(0), y(0), z(0) {}
};

bool from_file ( fstream & infile, PlayerRecord & in_rec )
{
    infile.read( reinterpret_cast<char*>(& in_rec.x ), sizeof( in_rec.x ) );
    infile.read( reinterpret_cast<char*>(& in_rec.y ), sizeof( in_rec.y ) );
    infile.read( reinterpret_cast<char*>(& in_rec.z ), sizeof( in_rec.z ) );
    
    if (!infile)
    {
        return false;
    }
    return true;
}

void print(const PlayerRecord & rec)
{
    cout << rec.x << "  " << rec.y << "  " << rec.z << "\n";
}

int main ()
{
    fstream a_file( "recs.bin", ios::binary | ios::in );

    PlayerRecord rec;
    
    cout << "reading\n";
    if (from_file(a_file, rec))
    {
        print(rec);
    }
    else
    {
        cout << "Error reading from file (1)" << endl;        
    }
    
    if (from_file(a_file, rec))
    {
        print(rec);
    }
    else
    {
        cout << "Error reading from file (2)" << endl;        
    }
    
    if (from_file(a_file, rec))
    {
        print(rec);
    }
    else
    {
        cout << "Error reading from file (3)" << endl;        
    }
    
}  


I'm aware this could be written more cleanly, it isn't intended as anything special, it's just a quick example. If others would like to contribute ideas , please do so.
To Chervil. How fast you are ! Maybe you are stronger than Alex Allen. I tried your two files. They work very well. I improve my C°° every day with you. Thanks again.
Topic archived. No new replies allowed.