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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
// Wiz.h
#ifndef WIZ_H
#define WIZ_H
#include <fstream>
#include <string>
class Wizard
{
public:
Wizard();
Wizard(std::string name, int hp, int mp, int armor);
// [...] other methods snipped
void print();
void save(std::ofstream& outFile);
void load(std::ifstream& inFile);
private:
std::string mName;
int mHitPoints;
int mMagicPoints;
int mArmor;
};
#endif
// Wiz.cpp
#include "Wiz.h"
#include <iostream>
using namespace std;
Wizard::Wizard()
{
mName = "Default";
mHitPoints = 0;
mMagicPoints = 0;
mArmor = 0;
}
Wizard::Wizard(std::string name, int hp, int mp, int armor)
{
mName = name;
mHitPoints = hp;
mMagicPoints = mp;
mArmor = armor;
}
void Wizard::print()
{
cout << "Name = " << mName << endl;
cout << "HP = " << mHitPoints << endl;
cout << "MP = " << mMagicPoints << endl;
cout << "Armor = " << mArmor << endl;
cout << endl;
}
void Wizard::save(ofstream& outFile)
{
outFile.write(mName.c_str(), mName.size());
outFile.write((char*)&mHitPoints, sizeof(int));
outFile.write((char*)&mMagicPoints, sizeof(int));
outFile.write((char*)&mArmor, sizeof(int));
}
void Wizard::load(ifstream& inFile)
{
string garbage;
inFile.read((char*)&mName, mName.size()); // read name
inFile.read((char*)&mHitPoints, sizeof(int)); // read hit points
inFile.read((char*)&mMagicPoints, sizeof(int)); // read magic points
inFile.read((char*)&mArmor, sizeof(int)); // read armor
}
// main.cpp
#include "Wiz.h"
#include <iostream>
using namespace std;
int main()
{
// Create wizards with specific data.
Wizard wiz0("Gandalf", 25, 100, 10);
Wizard wiz1("Loki", 50, 150, 12);
Wizard wiz2("Magius", 10, 75, 6);
// Create a stream which will transfer the data from our program
// to the specified file "wizdata.txt".
ofstream outFile("wizdata.txt", ios_base::binary);
// If the file opened correctly then call save methods.
if (outFile)
{
// Dump data into the stream
wiz0.save(outFile);
wiz1.save(outFile);
wiz2.save(outFile);
// Done with stream -- close it.
outFile.close();
}
// Create some blank wizards, which we will load data into
Wizard wiz4;
Wizard wiz5;
Wizard wiz6;
// Output the wizards before they are loaded.
cout << "BEFORE LOADING..." << endl;
wiz4.print();
wiz5.print();
wiz6.print();
// Create a stream which will transfer the data from the
// file "wizdata.txt" to our program
ifstream inFile("wizdata.txt", ios_base::binary);
// If the file opened correctly
if (inFile)
{
wiz4.load(inFile);
wiz5.load(inFile);
wiz6.load(inFile);
}
// Output the wizards to show the data was loaded correctly
cout << "AFTER LOADING..." << endl;
wiz4.print();
wiz5.print();
wiz6.print();
cin.ignore();
cin.get();
return 0;
}
|