第 9 章 c++ 的输入和输出

Click here to load reader

Upload: felix-wagner

Post on 30-Dec-2015

161 views

Category:

Documents


0 download

DESCRIPTION

第 9 章 C++ 的输入和输出. 9.1 C++ 为何建立自己的输入输出系统.  因为在 C++ 中用户需要定义众多的自定义类型,但是 C 语言的输入输出系统不支持用户自定义的类型。请看下面的类 : class my_class{ int i; float f; char *str; } obj; 对此类类型,在 C 语言中下面的语句是不能接受的 : printf( " % my_class " ,obj); - PowerPoint PPT Presentation

TRANSCRIPT

  • 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 9.3.1 C++cincout>>; // cout(); cout.operator
  • 9.3 1.
  • 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

  • 1 long ios::setf(long flags); .setf(ios::) 9_1 #include void main() { cout.setf(ios::showpos|ios::scientific); cout
  • 9_2 #includevoid showflags(long f){long i;for(i=0x8000;i;i=i>>1){if(i&f) cout
  • 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

  • 9_3 #include main() { cout
  • cout
  • : 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+

  • 9_4 #include#includemain(){cout
  • : 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_5 #include#includeostream& output1(ostream& stream){ stream.setf(ios::left); stream
  • 9_6 #include#includeistream& input1(istream& in){ in>>hex; coutinput1>>i; cout
  • 9.4.1
  • 9.4.2 >> >>: istream& operator>>(istream& inclass_name& obj) { in>>obj.item1; in>>obj.item2; . . . in>>obj.itemn; return in; } 9.4

  • 9.7
  • 9.8 >>#includeclass three_d {public: three_d(int a,int b,int c) { x=a; y=b; z=c; } friend ostream& operator(istream& itput,three_d& ob);private:int x,y,z;};ostream& operator
  • istream& operator>>(istream& input, three_d& ob){ coutob.x; input>>ob.y; input>>ob.z; return input;}main(){ three_d obj(10,20,30); // three_dobj coutobj; // obj cout
  • 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

  • 9.9 test#includeint main(){ ofstream fout("f:\\ccp\\909\\909.txt"); if (!fout){ cout
  • 2. (1) get()put() get(): istream& get(char& ch); get()ch0 put(): ostream& put(char ch); put()ch 9.5

  • 9.10 a z 26#include#includevoid test_write(){ ofstream fs("d:\\test.dat"); int i; char c='a'; for (i=0;i
  • (2) read()write() read()write(): istream &read(unsigned char* bufint num); ostream &write(const unsigned char* bufint num); 9.5

  • 9-11 write()test#include#include#includemain(){ ofstream out("f:\\ccp\\911\\test.dat"); if (!out) { cout
  • (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

  • 9.12 testX #include#includevoid main(void){ fstream fs; fs.open("d:\\test",ios::in| ios::out); // if (fs.fail()) cout
  • 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

  • 9.13#include"iostream.h"#include"strstrea.h"#include"string.h"char * parseString(char * pStr){int num1;float balace1;int len1=strlen("Num:")+strlen(",balace:")+strlen(pStr);istrstream inp(pStr,0); //ios::innullinp>>num1>>balace1; //char * pBuffer=new char[len1];memset(pBuffer,0,len1);ostrstream outp(pBuffer,len1);outp