# Include <iostream> # include <string> # include <fstream> using namespace STD; Class test {PRIVATE: String STR; int A; public: Test (); test (string STR, int A); void savetest () ;}; test: Test () {STR = "\ 0" ;}test: Test (string STR, int A) {This-> A = A; this-> STR = STR;} void test: savetest () {fstream OUTFILE, infile; OUTFILE. open ("testfile.txt", IOS: Out | IOs: Binary); If (! OUTFILE) {cout <"failed to open the system file! 1 \ n "; abort ();} OUTFILE. write (char *) This, sizeof (TEST); // <this-> A <this-> STR; OUTFILE. close (); infile. open ("testfile.txt", IOS: In | IOs: Binary); If (! Infile) {cout <"failed to open the system file! 2 \ n "; abort () ;}test * t = new test (); infile. read (char *) (T), sizeof (TEST); cout <t-> A <t-> STR <Endl; infile. close (); OUTFILE. open ("testfile.txt", IOS: Out | IOs: app | IOs: Binary); If (! OUTFILE) {cout <"failed to open the system file! 3 \ n "; abort ();} test * tes = new test (" Hi hi me? ", 11); OUTFILE. write (char *) TES, sizeof (TEST); OUTFILE. close (); infile. open ("testfile.txt", IOS: In | IOs: Binary); If (! Infile) {cout <"failed to open the system file! 4 \ n "; abort () ;}int I; test * TT [2]; for (I = 0; I <2; I ++) {TT [I] = new test (); infile. read (char *) TT [I], sizeof (TEST); cout <TT [I]-> A <TT [I]-> STR;} infile. close ();} void main () {test * test = new test ("Hello Hei! ", 12); test-> savetest ();}
Bytes --------------------------------------------------------------------------------------------------------------
It is best to use the class created on the stack, that is, the new income, so thatProgramFinal structure...