IOStreamLibrary
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&));
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);
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&));
ios bool good ( ) const; bool fail ( ) const; bool eof ( ) const; IOS-Member Beschreibung Beispiel bool good ( ) const; Check if the state of the stream is good for i/o while (IFile.good()) bool fail ( ) const; Check if either failbit or badbit is set while (!IFile.fail()) bool eof ( ) const; 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)) operator void*( )const; Convert to pointer while ((void*)IFile) iostate rdstate() const; Get error state flags void setstate(iostate s); 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).
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;
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; }
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;
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&));
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 { 19 IFile.getline(vBuf,128); 20 //cout<< f(cout) << vBuf << endl; 21 cout<< f << vBuf << endl; 22 line++; 23 }//erzeugt nebenstehende Ausgabe 24 else cerr<<"Mist"<<endl; 25 return 0; 26 }
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); };
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;