|
You try
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
typedef struct {
long long ID;
string name;
int r1, r2, r3;
} data, * pdata;
int main ()
{
data d;
d.ID = 5050379001;
d.name = "4k.grubby";
d.r1 = 85;
d.r2 = 87;
d.r3 = 83;
data d0, d1;
d0.ID = 5050379002;
d0.name = "john";
d0.r1 = d0.r2 = d0.r3 = 90;
d1.ID = 5050379003;
d1.name = "tom";
d1.r1 = d1.r2 = d1.r3 = 87;
ofstream File ("student.db", ios :: binary); // New binary file
File.write ((const char *) (&d), sizeof (data));
File.write ((const char *) (&d0), sizeof (data));
File.write ((const char *) (&d1), sizeof (data));
d1.name = "jerry"; // Modify the data
File.seekp (2 * sizeof (data)); // Move to the third group of data
File.write ((const char *) (&d1), sizeof (data)); // Overwrite
File.close ();
ifstream iFile ("student.db", ios :: binary);
data d2;
iFile.seekg (sizeof (data) * 2);
iFile.read ((char *) (&d2), sizeof (data));
cout << d2.name << d2.ID << endl;
iFile.close ();
} |
|