Die Präsentation wird geladen. Bitte warten

Die Präsentation wird geladen. Bitte warten

IOStreamLibrary.

Ähnliche Präsentationen


Präsentation zum Thema: "IOStreamLibrary."—  Präsentation transkript:

1 IOStreamLibrary

2 ostream ostream& ostream::put(char c);
ostream& ostream::write(const char* pStr, int n); ostream& ostream::write(const unsigned char* pStr, int n); ostream& ostream::write(const signed char* pStr, int n); ostream& operator<<(char C); ostream& operator<<(unsigned char C); ostream& operator<<(signed char C); ostream& operator<<(const char* pC); ostream& operator<<(const unsigned char* pC); ostream& operator<<(const signed char* pC); ostream& operator<<(short S); ostream& operator<<(unsigned short S); ostream& operator<<(int I); ostream& operator<<(unsigned int I); ostream& operator<<(long L); ostream& operator<<(unsigned long L); ostream& operator<<(float F); ostream& operator<<(double D); ostream& operator<<(long double D); ostream& operator<<(streambuf*); ostream& operator<<(ostream& (*f)(ostream&)); ostream& operator<<(ios& (*f)(ios&));

3 istream int istream::get();
istream&istream::get( char* pCh,int n, char Delim='\n'); istream&istream::get(unsigned char* pCh,int n, char Delim='\n'); istream&istream::get(signed char* pCh,int n, char Delim='\n'); istream&istream::get( char& rCh); istream&istream::get(unsigned char* rCh); istream&istream::get(signed char* rCh); istream&istream::getline( char& rCh); istream&istream::getline(unsigned char* rCh); istream&istream::getline(signed char* rCh); istream&istream::read( char* pCh, int Count); istream&istream::read(unsigned char* pCh, int Count); istream&istream::read(signed char* pCh, int Count);

4 istream& operator>>( char * pCh);
istream& operator>>(unsigned char * pCh); istream& operator>>(signed char* pCh); istream& operator>>( char& rCh); istream& operator>>(unsigned char& rCh); istream& operator>>(signed char& rCh); istream& operator>>( short& rSh); istream& operator>>(unsigned short& rSh); istream& operator>>( int& rI); istream& operator>>(unsigned int& rI); istream& operator>>( long& rL); istream& operator>>(unsigned long& rL); istream& operator>>(float& rF); istream& operator>>(double& rD); istream& operator>>(long double& rLD); istream& operator>>(streambuf* pSB); istream& operator>>(istream& (*_f)(istream&)); istream& operator>>(ios& (*_f)(ios&));

5 ios IOS-Member Beschreibung Beispiel
Check if the state of the stream is good for i/o while (IFile.good()) Check if either failbit or badbit is set while (!IFile.fail()) Check if eofbit is set while (!IFile.eof()) bool bad ( ) const; Check if badbit is set while (!Ifile.bad) bool operator ! ( ) const; Evaluate stream object while(!(!IFile)) Convert to pointer while ((void*)IFile) Get error state flags Set error state flag void clear (iostate state=goodbit ); Set error state flags flag value indicates eofbit End-Of-File reached while performing an extracting operation on an input stream. failbit The last input operation failed because of an error related to the internal logic of the operation itself. badbit Error due to the failure of an input/output operation on the stream buffer. goodbit No error. Represents the absence of all the above (the value zero).

6 Beispiel #include <iostream> #include <fstream>
using namespace std; int main() { ifstream IFile("Testeof.cpp"); char c=0; // while (!IFile.eof()) // while (IFile.good()) // while (!IFile.fail()) // while (!IFile.bad()) // while(!(!IFile)) // while ((void*)IFile) while (c!=EOF) cout<< (c=(char)IFile.get()); } return 0;

7 istream mit filebuf // istream constructor #include <iostream>
Funkton Anmerkung explicit istream (streambuf * sb); // istream constructor #include <iostream> #include <fstream> using namespace std; int main (int argc, char* argv[]) { filebuf fb; fb.open (argv[1],ios::in); istream is(&fb); if (fb.is_open()) { while (fb.in_avail()) cout << char(is.get()); fb.close(); }else cerr<<"could not open file " <<argv[1]<<endl; Return 0; }

8 get/getline /* Es wird nur die erste Zeile
gelesen, weil get das \n nicht extrahiert !! */ #include <iostream> #include <fstream> using namespace std; int main() { ifstream IFile("Testeof.cpp"); char vBuf[128]; if (IFile.is_open()) while (!IFile.eof()) IFile.get(vBuf,128); cout<< vBuf << endl; } else cerr<<"Mist"<<endl; return 0; /* Nimmst Du statt get, getline, funktioniert die Sache fein*/ #include <iostream> #include <fstream> using namespace std; int main() { ifstream IFile("Testeof.cpp"); char vBuf[128]; if (IFile.is_open()) while (!IFile.eof()) IFile.getline(vBuf,128); cout<< vBuf << endl; } else cerr<<"Mist"<<endl; return 0;

9 Manipulatoren ohne Parameter
Manipulatoren ohne Parameter werden durch eine Funktion ostream& f(ostream& os) { .... return os; } programmiert, deren Name (nicht deren Aufruf!!) dann in den Ausgabestream eingefügt wird. Andere Funktionen werden ausgeführt und ihr Returnwert entsprechend des Typs ausgegeben. ostream& operator<<(ostream& (*f)(ostream&));

10 Beispiel Manipulator #include <iostream>
#include <fstream> #include <iomanip> using namespace std; // Manipulator, Zeilennummern int line; ostream& f(ostream& os) { os<< setw(4)<<line <<' '; return os; } int main(int argc, char* argv[]) ifstream IFile(argv[1]); char vBuf[128]; if (IFile.is_open()) while (!IFile.eof()) IFile.getline(vBuf,128); //cout<< f(cout) << vBuf << endl;// Falsch!! cout<< f << vBuf << endl; // Richtig !! line++; }//erzeugt nebenstehende Ausgabe else cerr<<"Mist"<<endl; return 0; 0 #include <iostream> 1 #include <fstream> 2 #include <iomanip> 3 using namespace std; 4 // Manipulator, der nichts macht 5 int line; 6 ostream& f(ostream& os) 7 { 8 os<< setw(4)<<line <<' '; 9 return os; 10 } 11 12 int main(int arghc, char* argv[]) 13 { 14 ifstream IFile(argv[1]); 15 char vBuf[128]; 16 if (IFile.is_open()) 17 while (!IFile.eof()) 18 { IFile.getline(vBuf,128); //cout<< f(cout) << vBuf << endl; cout<< f << vBuf << endl; line++; 23 }//erzeugt nebenstehende Ausgabe 24 else cerr<<"Mist"<<endl; 25 return 0; 26 }

11 Die als Manipulator eingetragene Funktion wird ausgerufen
Sie erzeugt als Returnwert ein Objekt (im Bsp. nBlank) Dieses ManipulatorObjekt wird mit einem Funktionspointer und dem Parameterwert initialisiert Auf das Maipulatorobjekt wird der eigene operator<< angewandt Die operator<< Funktion ruft die übergebene Funktion mit dem Prameterwert des Manipulators #include <iostream> #include <fstream> #include <iomanip> using namespace std; class nBlank; ostream & nBlankFunc(ostream & os, int n); ostream& operator<<(ostream& DS, nBlank MO); typedef ostream& (ftype)(ostream& DS, int i); class nBlank { int i; ftype* f; public: nBlank(int j, ftype* X) { i = j; f = X; } friend ostream& operator<<(ostream& DS, nBlank DSL); };

12 nBlank blank(int n){return nBlank(n,nBlankFunc);}
ostream & nBlankFunc(ostream & os, int n) {for (int i=0; i<n;i++)os<<' '; return os;} ostream& operator<<(ostream& DS, nBlank MO) { return MO.f(DS,MO.i); } int main(int arggc, char*argv[]) ifstream IFile(argv[1]); char vBuf[128]; if (IFile.is_open()) while (!IFile.eof()) IFile.getline(vBuf,128); cout<< blank(3) << vBuf << endl; }//erzeugt nebenstehende Ausgabe else cerr<<"Mist"<<endl; return 0;


Herunterladen ppt "IOStreamLibrary."

Ähnliche Präsentationen


Google-Anzeigen