#include #include #include #include #include #include using std::cout; using std::endl; using std::copy; using std::string; using std::vector; using std::ostream; using std::setw; using std::setiosflags; using std::ofstream; using std::ifstream; using std::ios; using std::cerr; #define INT 1 #define FLOAT 2 #define STRING 3 // ---------------------------------------------------------------------- class Printable { public: int type; virtual void print(ostream &output) const = NULL; }; // ---------------------------------------------------------------------- class Int : public Printable { public: int value; void print(ostream &output) const { output << value << endl; } }; // ---------------------------------------------------------------------- class Float : public Printable { public: float value; void print(ostream &output) const { output << value << endl; } }; // ---------------------------------------------------------------------- class String : public Printable { public: string value; void print(ostream &output) const { output << value << endl; } }; // ---------------------------------------------------------------------- void PrintV (vector V) { for (int i = 0; i < V.size(); i++) V[i]->print(cout); } // ---------------------------------------------------------------------- void TextFileSaveV(vector V) { ofstream outPrintFile("print.txt", ios::out); if (!outPrintFile) { cerr << "File could not be opened." << endl; exit(1); } outPrintFile << setiosflags(ios::left) << setw(10) << "Type" << "Data" << endl; for (int i = 0; i < V.size(); i++) { outPrintFile << setiosflags(ios::left) << setw(10) << V[i]->type; V[i]->print(outPrintFile); } outPrintFile.close(); } // ---------------------------------------------------------------------- void BinaryFileSaveV(vector V) { ofstream outBinaryFile("output.dat", ios::out); int len; char* buf = NULL; if (!outBinaryFile) { cerr << "File could not be opened for writing." << endl; exit(1); } for (int i = 0; i < V.size(); i++) { outBinaryFile.write((const char*)(&(V[i]->type)), sizeof(int)); switch (V[i]->type) { case INT : outBinaryFile.write((const char*)(&(((Int*)(V[i]))->value)), sizeof(int)); break; case FLOAT : outBinaryFile.write((const char*)(&(((Float*)(V[i]))->value)), sizeof(float)); break; case STRING : len = ((String*)(V[i]))->value.length(); if (buf != NULL) delete [] buf; buf = new char[len + 1]; ((String*)(V[i]))->value.copy(buf, len, 0); buf[len] = NULL; outBinaryFile.write((const char*)buf, len+1); break; } } outBinaryFile.close(); } // ---------------------------------------------------------------------- void BinaryFileLoadV(vector &V) { Int* i; Float* f; String* s; char c; int type; ifstream inBinaryFile("output.dat", ios::in); if (!inBinaryFile) { cerr << "File could not be opened for reading." << endl; exit(1); } inBinaryFile.read((char*)(&type), sizeof(int)); while (!inBinaryFile.eof()) { switch (type) { case INT : i = new Int(); inBinaryFile.read((char*)(&(i->value)), sizeof(int)); V.push_back(i); break; case FLOAT : f = new Float(); inBinaryFile.read((char*)(&(f->value)), sizeof(float)); V.push_back(f); break; case STRING : s = new String(); inBinaryFile.read((char*)(&c), sizeof(char)); while (c != NULL) { s->value += c; inBinaryFile.read((char*)(&c), sizeof(char)); } V.push_back(s); break; } inBinaryFile.read((char*)(&type), sizeof(int)); } inBinaryFile.close(); } // ---------------------------------------------------------------------- void main (void) { vector V; Int i, i2; Float f, f2; String s, s2; i.type = i2.type = INT; i.value = 42; i2.value = 31676; f.type = f2.type = FLOAT; f.value = (float)97.3; f2.value = (float)-6945.435; s.type = s2.type = STRING; s.value = "Hello world!"; s2.value = "\"Come, let us go down and confuse their language so they will not understand each other.\" - Gen. 11:7"; V.push_back(&i); V.push_back(&f); V.push_back(&s); V.push_back(&f); V.push_back(&f); V.push_back(&i2); V.push_back(&f2); V.push_back(&s2); V.push_back(&i); V.push_back(&s2); cout << "Vector initially" << endl; PrintV(V); TextFileSaveV(V); BinaryFileSaveV(V); V.clear(); BinaryFileLoadV(V); cout << endl << "Vector after loading from binary file" << endl; PrintV(V); }