Written objects #include
Class book {public: void getData () {cout << "Enter book title:"; cin >> title; cout << "Enter):" CIN >> Author; cout << "Enter book sold per month: "; Cin >> Numsold;} private: char title [len]; char author [len]; int Numsold;
INT Main (int Argc, char * argv []) {OFStream Output ("IO.TXT"); book a; char ch; do {a.getdata (); Output.write ((char *) & a, sizeof (a ); Cout << "ENTER ANOTHER ANDE? (Y / N):"; cin >> ch;} while (ch == 'y'); return 0;} =========== ============================================== 读 写 v Include
Using namespace std;
Template
Template
Template
INT main () {// reads data IFSTREAM FIN ("Save.txt") from the file; Vector
#include
Int main () {ifstream fopen ("open.txt"); OFSTream fsave ('save.txt "); if (! fopen ||! fsave) {CERR <<" Unable to open file << end1; return - 1;} // read file data to Vector Vector
================================================= Basic reading Write an operation #include
Using Namespace std; int main () {char filename [30], name [30]; int number, score; OFSTREAM OUTSTUF; COUT << "please input the name of students file: / n"; cin >> filename; Outstuf .open (filename, ios :: out); if (! outstuf) {CERR << "File Could Not Be opened! << endl; abort ();
Outstuf << "this is a file of study ... / n"; cout << "Input the number, name, and score: (ENTER CTRL Z TO END INPUT) / N?"; While (CIN >> Number >> Name >> score) {Outstuf << Number << '/ t' << '/ "' << name << '/"' << '/ t' << score << '/ n'; cout << '?';} Outstuf.close (); returnif.} ================================= ============================ format reads #include
Struct Data {INT ID; // Register INT NUM; // Book Category CHAR NAME [10]; Float Price; Char Date [10]; Char Press [20]; int Buyn; int lendn; data * next;} void show (OFStream outf, data * hEAD) // Display content {data * p; p = head; OUTF << "/ nnow, the records are: / n"; if (head! = null) Do {OUTF < <"ID:" << P-> ID << "/ n" << "Num:" << p-> Num << "/ t" << "Name:" << p-> name << " / t "<<" Price: "<< p-> price <<" / t "<<" DATE: << P-> DATE << Endl << "Press:" << p-> press << Endl << "Buyn:" << p-> Buyn << "/ t" << "lendn:" << p-> lendn << Endl << Endl; P = P-> next;} while (p! = NULL);
Data * INSERT (Data * HEAD, DATA * DA) // Enter data {data * p0, * p1, * p2; p1 = head; p0 = da; if (head == null) {head = p0; p0-> Next = NULL;} else {while ((p0-> id> p1-> id) && p1-> next! = null) {p2 = p1; p1 = p1-> next;} if (p0-> id <= p1 -> id) {if (head == p1) {head = p0; p0-> next = p1;} else {p2-> next = p0; p0-> next = p1;}} else {p1-> next = P0; p0-> next = null;}} return head;}
Void main () {OFStream outf ("book.txt"); if (! outf) cout << "can't open file./n"; data * head, * p0; int N = 0; Head = NULL; For (;;) {n ; cout << "infut the" << n << "DATA: / N"; p0 = new data; cout << "ID:"; cin >> p0-> id; if (IF) P0-> ID == 0) {Delete [] P0; Break;} cout << "Num:"; cin >> p0-> Num; cout << "Name:"; cin >> p0-> name; cout << "Price:"; CIN >> P0-> Price; cout << "Data:"; cin >> p0-> date; cout << "press:"; cin >> p0-> press; cout << "Buyn:"; cin >> p0-> Buyn; cout << "lendn:"; cin >> p0-> lendn ;; head = INSERT (head, p0); // call function inserted data
Cout << Endl;
Show (outf, head); // call the function writes the data into the file outf.close ();
IFSTREAM INF ("BOOK.TXT"); // Output to Display IF (! INF) COUT << "Can't Open THE FILE./N"; Char C; cout << "/ nthe bookdata is: / N / N "; While (INF.GET (C)) // Displays the data to the display Cout << C; cout << endl; infl.close ();