C ++ IO operation

xiaoxiao2021-03-06  87

Written objects #include #include const INT LEN = 80;

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 #include #include #include #include

Using namespace std;

Template Struct Point {T x, Y; Explicit Point (Const T & X = 0, Const T & Y = 0): X (x), y (y) {}};

Template Ostream & Operator << (Ostream & OS, Const Point & P) {OS << '' << p.x << '' << p.y; return os;}

Template IStream & Operator >> (ISTREAM & IS, POINT & P) {IS >> P.X >> P.Y; RETURN IS;}

INT main () {// reads data IFSTREAM FIN ("Save.txt") from the file; Vector > v; copy (istream_iterator > (fin), istream_iterator >> (), Back_inserter (v)); // Print Data Copy (v.begin (), v.end (), Ostream_Iterator >)); // Deposit File ofStream Fout ("out.txt"); copy (v.begin (), v.end (), ostream_iterator >)); cout << "press Enter to EXIT Cin.get ();

#include #include #include using namespace std;

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 V (ISTREAM_ITERATOR (), iStream_iterator ()); // Deposit file copy (v.begin (), v.end (), ostream_iterator (fsave, ")); return 0;}

================================================= Basic reading Write an operation #include #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 #include using namespace std; void main () { Char * lp [2]; int i = 0; char s [10] = "s"; ifstream in ("in.txt"); while (! in.eof ()) {char Order [10]; char content1 [30]; Char Content2 [30]; Char Other [10]; In.GetLine (Order, Sizeof (ORDER), '('); if (Strcmp (S, Order) == 0) {in.getLine (Content1 , SIZEOF (Content1), ','); In.GetLine (Content2, Sizeof (Content2), '); I = * Content1-48; LP [i] = Content2; Cout << "LP [" << I << "]" << lp [i] << endl;} in.getline (Other), '/ n');} in.close ();} ======== =================================================== Link table, IO # include #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 ();

转载请注明原文地址:https://www.9cbs.com/read-106651.html

New Post(0)