9 C++
9.1 C++ C++C: class my_class{ int i; float f; char *str; } obj; C: printf(% my_class,obj); C++C
9.2 C++ 9.2.1 C++ C++ :();();()
9.2 C++ C++: cin cout cerr () clog ()
9.2.2 C++streambufios : ios: streambuf
9.3 2. >>>> istream(cin): int x; cin>>x; xx
9.3 1>>234
9.3.2 C++ ios 9.3
1. ios iospublic 9.3
enum{ skipws =0x0001 / / left =0x0002 // right =0x0004 // internal =0x0008 // dec =0x0010 // oct =0x0020 // hex =0x0040 // showbase =0x0080 // showpoint =0x0100 // 9.3
uppercase =0x0200 //, // showpos =0x0400 // scientific =0x0800 // fixed =0x1000 // unitbuf =0x2000 // stdio =0x4000 //stdoutstderr }; 9.3
:1: skipws 0x0001 0000 0000 0000 0001 left 0x0002 0000 0000 0000 0010 right 0x0004 0000 0000 0000 0100 9.3
9-1
9.3
4 long ios::width(); long ios::width(int w); 0. 5 long ios::fill(); long ios::fill(char ch); ch 6 long ios::precision(int p); 9.3
: x_width=0 x_fill= x_precision=0 123 123.45678 ----------------------------- *** x_width=10, x_fill= , x_precision=4 *** 123 123.4568 234.567 x_width=0 x_fill= x_precision=4 ----------------------------- *** x_width=10, x_fill=&, x_precision=4 *** &&&&&&&123 123.4568 123&&&&&&& 123.4568 x_width=0 x_fill=& x_precision=4 9.3
iosI/OI/OC++:(1) dec (2) hex (3) oct (4) ws (5) endl (6) ends \0(7) flush 2.I/O
9.3
(8) setbase(int n) nn0810 16, n0(9) resetiosflags(long f) f(10) setiosflags(long f) f(11) setfill(int ch) ch,(12) setprecision(int n) ,(13) setw(int n) n iomanip.h 9.3
setiosflags()resetiosflags()
ios::setfill(c)ios::setw(n)ios::setprecision(n)ios::skipwsios::leftios::rightios::internalios::decios::octios::hexios::showbaseios::showpointios::uppercaseios::showposios::scientificios::fixedcnn00x0X0A~FE+
: 123567 123 1.234567e+02 123 7b 7b 173 173 123 123.4568 123##### $$$$$456 9.3
: ostream& manip_name(ostream& stream) { // return stream; } 9.4
: istream& manip_name(istream& stream) { // return stream; } 9.4
9.4.2 >> >>: istream& operator>>(istream& inclass_name& obj) { in>>obj.item1; in>>obj.item2; . . . in>>obj.itemn; return in; } 9.4
9.5 : (1) fstream.h; (2) : ifstream in; ofstream out; fstream both; in;outboth
9.5 (3) open()open()fstream.h, : void open(const unsigned char*int modeint access=filebuf::openprot); (4) (5)
9.5 9.5.1 1. ;C++;
9.5
9.5 0 1 2 4 8 ofstream out;out.open(test,ios::out,0); out.open(test); ofstream out(test);
9.5 2. close()close() : out.close(); out
9.5.2 1. cincout 9.5
2. (1) get()put() get(): istream& get(char& ch); get()ch0 put(): ostream& put(char ch); put()ch 9.5
(2) read()write() read()write(): istream &read(unsigned char* bufint num); ostream &write(const unsigned char* bufint num); 9.5
(3) EOF(End OF)eof(): int eof(); eof(): ifstream ifs; if (!ifs.eof()) // ifstream ifs; if(ifs)//
9.5
(4) C++I/O get; put 9.5
putostream& seekp(streampos pos);ostream& seekp(streamoff off,ios::seek_dir dir);getistream& seekg(streampos pos); //istream& seekg(streamoff off,ios::seek_dir dir); //diroffdir3ios::beg offios::cur offios::end offdatafile.seekg(-20L,ios::cur); //20
9.5
5 istrstreamostrstreamstrstreamstrstrea.h 1istrstream istrstream::istrstream(const char * str); istrstream::istrstream(const char * str,int nLength); 12nLength0istrstreamstr 2ostrstream ostrstream::ostrstream(char * str,int nLength,int nMode=ios::out); 123 9.5