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;
}
|