| |
| 1 |
|
| 2 |
#include <iostream> |
| 3 |
#include <fstream> |
| 4 |
#include <string> |
| 5 |
using namespace std; |
| 6 |
//#include <malloc.h> |
| 7 |
class A |
| 8 |
{ |
| 9 |
private: |
| 10 |
int a; |
| 11 |
int b; |
| 12 |
int *ptr; |
| 13 |
string name; |
| 14 |
public: |
| 15 |
A():a(10),b(20){ptr=NULL;} |
| 16 |
A(int x,int y):a(x),b(y){ptr = new int;*ptr = 100; } |
| 17 |
~A() {delete ptr;} |
| 18 |
void setname(string str){name = str;} |
| 19 |
int geta(){return a;} |
| 20 |
int getb(){return b;} |
| 21 |
string getname(){return name;} |
| 22 |
}; |
| 23 |
|
| 24 |
int main() |
| 25 |
{ |
| 26 |
// char *str, *str2; |
| 27 |
int t=10; |
| 28 |
int *ptr = &t; |
| 29 |
int *&rp=ptr; |
| 30 |
string name; |
| 31 |
char str[5]; |
| 32 |
A aobj(12,14); |
| 33 |
A ao; |
| 34 |
ofstream fo("file.txt",ios_base::out); |
| 35 |
ifstream fi; |
| 36 |
|
| 37 |
cin>>name; |
| 38 |
aobj.setname(name); |
| 39 |
cout << "writing into file"; |
| 40 |
int a; |
| 41 |
a = aobj.geta(); |
| 42 |
int *p; |
| 43 |
p = &a; |
| 44 |
fo<< aobj.getname();//<<(char *)aobj.getb()<<(char *)aobj.getname(); |
| 45 |
fo.close(); |
| 46 |
fi.open("file.txt",ios_base::in); |
| 47 |
fi.read(str,4); |
| 48 |
str[4]='\0'; |
| 49 |
cout<<str; |
| 50 |
|
| 51 |
|
| 52 |
//str = (char*)malloc(10); |
| 53 |
//str2 = (char*)malloc(128); |
| 54 |
|
| 55 |
//free(str); |
| 56 |
} |
| 57 |
#if 0 |
| 58 |
class Date |
| 59 |
{ |
| 60 |
private: |
| 61 |
int day; |
| 62 |
int month; |
| 63 |
int year; |
| 64 |
//constructor and destructor |
| 65 |
public: |
| 66 |
Date(); //current date |
| 67 |
~Date(); |
| 68 |
//... |
| 69 |
}; |
| 70 |
//Storing a Date object is a rather straightforward operation: Every data member is written to a persistent stream |
| 71 |
//(usually this is a local disk file, but it can also be a file on a remote computer). The data members can be read from |
| 72 |
//the stream at a later stage. For that purpose, two additional member functions are required, one for storing the object |
| 73 |
//and the other for reading the stored object: |
| 74 |
#include<fstream> |
| 75 |
using namespace std; |
| 76 |
class Date |
| 77 |
{ |
| 78 |
//... |
| 79 |
virtual ofstream& Write(ofstream& archive); |
| 80 |
virtual ifstream& Read(ifstream& archive); |
| 81 |
}; |
| 82 |
ofstream& Date::Write(ofstream& archive) |
| 83 |
//ANSI/ISO C++ Professional Programmer's Handbook - Chapter 14 - Concluding Remarks and Future Directions |
| 84 |
//file:///D|/Cool Stuff/old/ftp/1/1/ch14/ch14.htm (8 von 18) [12.05.2000 14:46:48] |
| 85 |
{ |
| 86 |
archive.write( reinterpret_cast<char*> (&day), sizeof(day)); |
| 87 |
archive.write( reinterpret_cast<char*> (&month), sizeof(month)); |
| 88 |
archive.write( reinterpret_cast<char*> (&month), sizeof(year)); |
| 89 |
return archive; |
| 90 |
} |
| 91 |
ifstream& Date::Read(ifstream& archive) |
| 92 |
{ |
| 93 |
archive.read( reinterpret_cast<char*> (&day), sizeof(day)); |
| 94 |
archive.read( reinterpret_cast<char*> (&month), sizeof(month)); |
| 95 |
archive.read( reinterpret_cast<char*> (&month), sizeof(year)); |
| 96 |
return archive; |
| 97 |
} |
| 98 |
In addition to the member functions Read() and Write(), it is necessary to define a reconstituting constructor, |
| 99 |
which reads a serialized object from a stream: |
| 100 |
Date::Date(ifstream& archive) //reconstituting constructor |
| 101 |
{ |
| 102 |
Read(arcive); |
| 103 |
} |
| 104 |
#endif |
| |