, , . , , , . 10.4.2.1 - : // Simple manipulators: ios& oct(ios&); // ios& dec(ios&); // ios& hex(ios&); // ostream& endl(ostream&); // '\n' ostream& ends(ostream&); // '\0' ostream& flush(ostream&); // istream& ws(istream&); // // : SMANIP<int> setbase(int b); SMANIP<int> setfill(int f); SMANIP<int> setprecision(int p); SMANIP<int> setw(int w); SMANIP<long> resetiosflags(long b); SMANIP<long> setiosflags(long b); , cout << 1234 << ' ' << hex << 1234 << ' ' << oct << 1234 << endl; 1234 4d2 2322 cout << setw(4) << setfill('#') << '(' << 12 << ")\n"; cout << '(' << 12 << ")\n"; (##12) (12) <iomanip.h>, . 10.4.3 ostream ostream , ios. class ostream : public virtual ios { //... public: ostream& flush(); ostream& seekp(streampos); ostream& seekp(streamoff, seek_dir); streampos tellp(); //... }; , flush() . ostream . p , . , -, , . streampos , streamoff , seek_dir. ios: class ios { //... enum seek_dir { beg=0, // cur=1, // end=2 // }; //... }; 0, n : char file[n-1]; fout file, fout.seek(10); fout<<'#'; # file[10]. 10.4.4 istream ostream, iostream, ios. class istream : public virtual ios { //... public: int peek() istream& putback(char c); istream& seekg(streampos); istream& seekg(streamoff, seek_dir); streampos tellg(); //... }; ostream. g , . p g , iostreams ostream istream, . peek() , , . putback(), $$10.3.3, , . 10.5  . : #include <fstream.h> #include <libc.h> void error(char* s, char* s2 ="") { cerr << s << ' ' << s2 << '\n'; exit(1); } int main(int argc, char* argv[]) { if (argc != 3) error("wrong number of arguments"); ifstream from(argv[1]); if (!from) error("cannot open input file",argv[1]); ostream to(argv[2]); if (!to) error("cannot open output file",argv[2]); char ch; while (from.get(ch)) to.put(ch); if (!from.eof() || to.bad()) error("something strange happened"); return 0; } ofstream - , . , ifstream - , . , , , , . ifstream , ofstream . ostream istream , : class ios { public: //... enum open_mode { in=1, // out=2, // ate=4, // app=010, // trunc=020, // nocreate=040, // , noreplace=0100 // , }; //... }; open_mode . , . . , , , : void f() { ofstream mystream(name,ios::out|ios::nocreate); if (ofstream.bad()) { //... } //... } : fstream dictionary("concordance", ios::in|ios::out); , ostream ostream, fstream. , fstream iostream, , , istream ostream. , ostream istream ios, , . istream ostream - seekp() seekg(). iostream . 10.5.1  , close() : mystream.close(); , close() , . , cout, cin cerr . , <iostream.h> - . , - , . , , , . , , , , <iostream.h> : class Io_init { static int count; //... public: Io_init(); ^Io_init(); }; static Io_init io_init ; io_init. io_init Io_init::count , - : Io_init::Io_init() { if (count++ == 0) { // cout // cerr // cin // .. } } , io_init Io_count, , : Io_init::^Io_init() { if (--count == 0) { // cout (, ..) // cerr (, ..) // cin // .. } } , . ++ . . , , . , , , . , , , . , , , ( Io_init::count) . , . 10.5.2  , , .. , , , , . . , ostrstream , : char* p = new char[message_size]; ostrstream ost(p,message_size); do_something(arguments,ost); display(p); do_something ost, ost .. , ost , fail(). display "" . , , , , . , ost . , istrstream , , : void word_per_line(char v[], int sz) /* "v" "sz" */ { istrstream ist(v,sz); // istream v char b2[MAX]; // while (ist>>b2) cout <<b2 << "\n"; } . <strstream.h>. 10.5.3  - , . , ostream, , , ostream, . . , ostream , . , , , . , , . <iostream.h> : class streambuf { // protected: char* base; // char* pptr; // char* gptr; // char* eptr; // char alloc; // , "new" //... // : // EOF , 0 - virtual int overflow(int c = EOF); // : // EOF , // virtual int underflow(); //... public: streambuf(); streambuf(char* p, int l); virtual ~streambuf(); int snextc() // { return (++gptr==pptr) ? underflow() : *gptr&0377; } int allocate(); // //... }; streambuf . , , . , ; ( ) -. overflow() underflow() , : class filebuf : public streambuf { protected: int fd; // char opened; // public: filebuf() { opened = 0; } filebuf(int nfd, char* p, int l) : streambuf(p,l) { /* ... */ } ~filebuf() { close(); } int overflow(int c=EOF); int underflow(); filebuf* open(char *name, ios::open_mode om); int close() { /* ... */ } //... }; int filebuf::underflow() // "fd" { if (!opened || allocate()==EOF) return EOF; int count = read(fd, base, eptr-base); if (count < 1) return EOF; gptr = base; pptr = base + count; return *gptr & 0377; // &0377 } streambuf. 10.6 -  ++ , - ++ - printf . , .. - ++, - . - . - ++ . - , . ++ - - ios::sync_with_stdio(). , printf() , . int printf(const char* format, ...) , format. : , , , . %, printf("there were %d members present.",no_of_members); %d , no_of_members . no_of_members==127, there were 127 members present. . % : - , ; d , ; , , ( , ); , , ; . ; d , , e f, ; * * . , ; h h , d, o, x u ; l l , d, o, x u ; % , %; ; c , . : d ; o ; x ; f [-]ddd.ddd, . , ; 0, ; e [-]d.ddde+dd; , ; ; g d, f e, ; c . ; s ( ), , ; , 0 , ; p ; u . , , . , . : char* src_file_name; int line; char* line_format = "\n#line %d \"%s\"\n"; main() { line = 13; src_file_name = "C++/main.c"; printf("int a;\n"); printf(line_format,line,src_file_name); printf("int b;\n"); } int a; #line 13 "C++/main.c" int b; printf() , . , - : char x; // ... printf("bad input char: %s",x); , . , getchar() : int i;: while ((i=getchar())!=EOF) { // C // i } : EOF int , getchar() int, char. - " ". 10.7  1. (*1.5) , , . 2. (*1.5) name_and_address (__). << >>. name_and_address. 3. (*2) . : , , , , , , .. . 4. (*1.5) , : (1) , (2) , (3) , (4) , ++, (5) , (6) , (7) , (8) , , , (9) . 5. (*4) - (<stdio.h>) - ++ (<iostream.h>). 6. (*4) - ++ (<iostream.h>) - (<stdio.h>). 7. (*4) ++ , . 8. (*2) , [] , . 9. (*3) 8, , [] . : [] " ", : , char . 10.(*2) 9, [] , . 11.(*3.5) . printf(). , . istream. 12.(*4) ( ) . 13.(**2) based : , , . , based(2,9) 1001. 14.(**2) "" -, istream, ostream, ifstream, ofstream , operator<<() operator>>() , , open() close() . , , . 15.(**2) , .  *  " ." - . . , . , . , . , . . 11.1  - . . , , , , . , "" : " , ". , , . , , , . , . , , . , . "" , , . , . , , . , , (, 10000 , ), . , . , " " , . , ++, , 12 13. , ++, . , , , , , , , . , . . ++ . , , ++ , ++. , ++. - . 12 ++, , "" ++, ++ ($$12.1). , : - , , - , . - - - . - , , , . - . - - . - , : , - . - . , . . . , - , . : - ; - . " , , ". ! , , , . , , - . , . , . - , , . , , " ", , . " " . " " , . : , , , . . , , ++, , , , : , "" . , 12 13, , , , , . $$11.2, : - $$11.3 . - $$11.4 . 12, 13 ++. , . , , , , , . , , . - , - . , , $$11.4.2. . : . , , , . . , : , - ( ) . , , . ( ) , . , , , . - , . , , - (. $$11.3.3, $$12.2.1 $$13.3). . , , . , . , . . , , , . , , . . [2]. . "" , : , , - , , , , , , . 11.2  - , . , , . ? , , . , - , , - . , , , ? . : - , - , - , - , - - . , , , , . . - , . , , , . , . , , "", . . (" , "). , , (" , "). , . - , . , , . , , . , . , () ("") , ( ) . , , "" . , , . : - , , - ; - , , . , - . , , - , , . , , . , . , . ( , ), , , , .. . . . , , . , . -