I/O-Streams

Vordefinierte Ströme

Jedem C++-Programm stehen die Standard-Eingabe (stdin), die Standard-Ausgabe (stdout) und die Standard-Fehlerausgabe (stderr) in Form folgender globaler Objekte zur Verfügung:

#include <iostream>

extern istream cin;  // stdin
extern ostream cout; // stdout
extern ostream cerr; // stderr
extern ostream clog; // stdlog

Schnittstelle

Abstraktion eines gepufferten Datenstroms

#include <streambuf>
std::streambuf typedef std::basic_streambuf<char>
public:
typedef typename Traits::int_type int_type;
typedef typename Traits::pos_type pos_type;
typedef typename Traits::off_type off_type;
virtual ~streambuf (); Destruktor
locale getloc () const;
locale pubimbue (const locale & loc);
streambuf * pubsetbuf (char * s, streamsize n); Externen Puffer verwenden
Positionierung für wahlfreien Zugriff
pos_type pubseekoff (off_type off,
ios_base::seekdir way,
ios_base::openmode = ios_base::in | ios_base::out);
pos_type pubseekpos (pos_type, ios_base::openmode = ios_base::in | ios_base::out);
streamsize in_avail ();
int_type sbumpc (); Aktuelles Zeichen verbrauchen
int_type sgetc (); Aktuelles Zeichen ansehen
streamsize sgetn (char * s, streamsize n); Zeichenkette lesen
int_type sputbackc (char c); Zeichen zurückschreiben
int_type sungetc (); Leseposition zurückrücken
int_type snextc ();
int_type sputc (char c); Zeichen schreiben
streamsize sputn (const char * s, streamsize n); Zeichenkette schreiben
int pubsync ();
protected:
streambuf(); undefined
Lesepuffer (get area)
void setg (char * gbeg_arg, char * gnext_arg, char * gend_arg);
char * eback () const; Anfang des Lesepuffers
char * gptr () const; Leseposition
char * egptr () const; Ende des Lesepuffers
Schreibpuffer (put area)
void setp (char *, char *);
char * pbase () const; Anfang des Schreibpuffers
char * pptr () const; Schreibposition
char * epptr () const; Ende des Schreibpuffers
void gbump (int n);
void pbump (int n);
virtual void imbue (const locale & loc);
virtual streambuf * setbuf (char * s, streamsize n);
Positionierung
virtual pos_type seekoff (off_type, ios_base::seekdir, ios_base::openmode = ios_base::in | ios_base::out);
virtual pos_type seekpos (pos_type, ios_base::openmode = ios_base::in | ios_base::out);
virtual int sync ();
virtual int showmanyc ();
Lesen
virtual streamsize xsgetn (char * s, streamsize n);
virtual int_type underflow (); Lesepuffer nachfüllen
virtual int_type uflow (); ruft underflow () auf
virtual int_type pbackfail (int_type = Traits::eof ());
Schreiben
virtual streamsize xsputn (const char * s, streamsize n);
virtual int_type overflow (int_type eof = Traits::eof (); Schreibpuffer leeren

ios_base

std::ios_base
public:
typedef T1 fmtflags;
typedef T2 iostate;
typedef T3 openmode;
typedef T4 seekdir;
typedef std::streamoff streamoff;
typedef std::streampos streampos;
enum event { copyfmt_event, erase_event, imbue_event };
Konstanten
static const fmtflags boolalpha, dec, fixed, hex, internal, left, oct, right, scientific, showbase, showpoint, showpos, skipws, unitbuf, uppercase, adjustfield, basefield, floatfield;
static const iostate badbit, eofbit, failbit, goodbit;
static const openmode app, ate, binary, in, out, trunc;
static const seekdir beg, cur, end;
static const event copyfmt_event, erase_event, copyfmt_event;
Methoden
ios_base & operator= (const ios_base & right);
fmtflags flags () const;
fmtflags flags (fmtflags newfmtflags);
fmtflags setf (fmtflags newfmtflags);
fmtflags setf (fmtflags newfmtflags, fmtflags mask);
void unsetf (fmtflags mask);
streamsize precision () const;
streamsize precision (streamsize newprecision);
streamsize width () const;
streamsize width (streamsize newwidth);
locale imbue (const locale & loc);
locale getloc () const;
protected:
ios_base ();

ios

#include <ios>
std::ios : std::ios_base
public:
typedef Traits::int_type int_type
typedef Traits::pos_type pos_type
typedef Traits::off_type off_type
operator void * () const;
bool operator! () const;
iostate rdstate () const;
void clear (iostate = goodbit);
void setstate (iostate state);
bool good () const;
bool eof () const;
bool fail () const;
bool bad () const;
void exceptions (iostate);
iostate exceptions () const;
explicit ios (streambuf * sb);
virtual ~ios ();
ostream * tie () const;
ostream * tie (ostream * os);
streambuf * rdbuf () const;
streambuf * rdbuf (streambuf * sb);
ios & copyfmt (const ios&);
char fill () const;
char fill (char);
locale imbue (const locale&);
char narrow (char, char) const;
char widen (char) const;
protected:
ios (); undefined
void init (streambuf * sb); undefined

Manipulatoren

Manipulatoren filtern die Ausgabe in einen Datenstrom und ermöglichen damit die von printf bekannten Formatierungen.

#include <iomanip>

std::cout << std::setfill ('0') << std::setw (4) << std::hex << 42 << "\n"; // entspricht "%04x", ergibt "002a"
Manipulator Reset Methode Beschreibung
std::flush Ausgabepuffer leeren
std::endl Zeilenumbruch ausgeben und Ausgabepuffer leeren
std::ends NUL-Terminierung ausgeben und Ausgabepuffer leeren
std::skipws std::noskipws ios::skipws Leerzeichen im Eingabestrom ignorieren
ios::unitbuf Ausgabepuffer nach jeder Schreiboperation leeren
std::boolalpha std::noboolalpha ios::boolalpha Wahrheitswerte als "true" und "false" ausgeben (C locale)
std::showpos std::noshowpos ios::showpos Vorzeichen auch bei positiven Zahlen ausgeben
std::dec ios::dec Zahlen im Dezimalsystem ausgeben
std::hex ios::hex Zahlen im Hexedzimalsystem ausgeben
std::oct ios::oct Zahlen im Oktalsystem ausgeben
std::showbase std::noshowbase ios::showbase Hexadezimalzahlen mit führendem "0x" ausgeben
std::showpoint std::noshowpoint ios::showpoint Dezimaltrennzeichen immer ausgeben
std::fixed ios::fixed 0.0123
std::scientific ios::scientific Wissenschaftliche Notation: 1.23e-02
std::uppercase std::nouppercase ios::uppercase Präfixe und Exponenten in Großbuchstaben
std::right ios::right Rechtsbündige Ausgabe: " -42"
std::left ios::left Linksbündige Ausgabe: "-42 "
std::internal ios::internal FIXME "- 42"
std::setw (n) std::setw (0) ostream.width (n) Mindestbreite
std::setfill (c) std::setfill (' ') ostream.fill (c) Füllzeichen
std::setprecision (n) ostream.precision (n) Set precision for floating point numbers
std::setbase (b) Entspricht oct, dec, hex für b ∈ { 8, 10, 16 }
std::setiosflags (f)
std::resetiosflags (f)

Eingabestrom

std::istream : std::ios
public:
explicit istream (streambuf * sb);
virtual ~istream ();
istream & operator>> (streambuf * sb);
istream & operator>> (bool & val);
istream & operator>> (short & val);
istream & operator>> (unsigned short & val);
istream & operator>> (int & val);
istream & operator>> (unsigned int & val);
istream & operator>> (long & val);
istream & operator>> (unsigned long & val);
istream & operator>> (void * & val);
istream & operator>> (float & val);
istream & operator>> (double & val);
istream & operator>> (long double & val);
streamsize gcount () const;
int_type get ();
istream & get (char & ch);
istream & get (char * s, streamsize n);
istream & get (char * s, streamsize n, char delim);
istream & get (streambuf & strbuf);
istream & get (streambuf & strbuf, char delim);
istream & getline (char * s, streamsize n);
istream & getline (char * s, streamsize n, char delim);
istream & ignore (streamsize n = 1, int_type delim = traits_type::eof ());
int_type peek ();
istream & read (char * s, streamsize n);
streamsize readsome (char * s, streamsize n);
istream & putback (char ch);
istream & unget ();
pos_type tellg ();
istream & seekg (pos_type pos);
istream & seekg (off_type off, ios_base::seek_dir way);
int sync ();

Ausgabestrom

std::ostream : std::ios
public:
ostream (streambuf *strbuf);
virtual ~ostream ();
ostream & operator<< (streambuf *strbuf);
ostream & operator<< (bool val);
ostream & operator<< (short val);
ostream & operator<< (unsigned short val);
ostream & operator<< (int val);
ostream & operator<< (unsigned int val);
ostream & operator<< (long val);
ostream & operator<< (unsigned long val);
ostream & operator<< (float val);
ostream & operator<< (double val);
ostream & operator<< (long double val);
ostream & operator<< (const void *val);
ostream & put (char ch);
ostream & write (char * s, streamsize n);
ostream & flush ();
pos_type tellp ();
ostream & seekp (pos_type pos);
ostream & seekp (off_type off, ios_base::seek_dir way);

Kombinierter Ein-/Ausgabestrom

std::iostream : std::istream, std::ostream
public:
explicit iostream (streambuf & *);
virtual ~iostream ();

File Streams

File Streams implementieren die Schnittstelle für Datenströme auf der Basis von Dateien.

#include <fstream>

Dateibasierter gepufferter Datenstrom

std::filebuf : std::streambuf
public:
filebuf ();
bool is_open () const;
filebuf * open (const char * filename, ios_base::openmode mode);
filebuf * close ();
protected:
virtual pos_type seekoff (off_type off, ios_base::seekdir way, ios_base::openmode which = ios_base::in | ios_base::out);
virtual pos_type seekpos (pos_type pos, ios_base::openmode which = ios_base::in | ios_base::out);
virtual int_type underflow ();
virtual int_type pbackfail (int_type meta = traits_type::eof ());
virtual int_type overflow (int_type meta = traits_type::eof ());
virtual int sync ();
virtual streambuf * setbuf (char * s, streamsize n);

Lesen aus Datei

std::ifstream : std::istream
public:
filebuf * rdbuf () const;
ifstream ();
explicit ifstream (const char * filename, ios_base::openmode mode = ios_base::in);
bool is_open () const;
void open (const char * filename, ios_base::openmode mode = ios_base::in);
void close ();

Schreiben in Datei

std::ofstream : std::ostream
public:
filebuf * rdbuf () const;
ofstream ();
explicit ofstream (const char * filename, ios_base::openmode mode = ios_base::out);
bool is_open () const;
void open (const char * filename, ios_base::openmode mode = ios_base::out);
void close ();

Lesen und Schreiben einer Datei

std::fstream : std::iostream
public:
fstream ();
explicit fstream (const char * filename, ios_base::openmode mode = ios_base::in | ios_base::out);
filebuf * rdbuf () const;
bool is_open () const;
void open (const char * filename, ios_base::openmode mode = ios_base::in | ios_base::out);
void close ();

String Streams

String Streams implementieren die Schnittstelle für Datenströme auf der Basis speicherbasierter Puffer.

#include <sstream>

String buffer

std::stringbuf : std::streambuf
public:
stringbuf (ios_base::openmode mode = ios_base::in | ios_base::out);
stringbuf (const std::string & str, ios_base::openmode mode = ios_base::in | ios_base::out);
std::string str () const;
void str (const std::string& newstr);
protected:
virtual pos_type seekoff (off_type off, ios_base::seekdir way, ios_base::openmode mode = ios_base::in | ios_base::out);
virtual pos_type seekpos (pos_type sp, ios_base::openmode mode = ios_base::in | ios_base::out);
virtual int_type underflow ();
virtual int_type pbackfail (int_type meta = traits_type::eof ());
virtual int_type overflow (int_type meta = traits_type::eof ());

Lesen aus Zeichenkette

std::istringstream : std::istream
public:
explicit istringstream (ios_base::openmode mode = ios_base::in);
explicit istringstream (const std::string& str, ios_base::openmode mode = ios_base::in);
stringbuf * rdbuf () const;
std::string str ();
void str (const std::string& newstr);

Schreiben in Zeichenkette

std::ostringstream : std::ostream
public:
explicit ostringstream (ios_base::openmode mode = ios_base::out);
explicit ostringstream (const std::string& str, ios_base::openmode mode = ios_base::out);
stringbuf * rdbuf () const;
std::string str ();
void str (const std::string& newstr);

Lesen und Schreiben einer Zeichenkette

std::stringstream : std::iostream
public:
explicit stringstream (ios_base::openmode mode = ios_base::in | ios_base::out);
explicit stringstream (const std::string& str, ios_base::openmode mode = ios_base::in | ios_base::out);
stringbuf * rdbuf () const;
std::string str ();
void str (const std::string & newstr);

Rezepte

String-Streams

Als Erstz für snprintf und sscanf.

#include <sstream>

std::ostringstream os ("Hello");
os << ", World!";
std::cout << os.str () << std::endl; // prints "Hello, World!"

std::string a, b;
std::istringstream is ("Hello, World!");
is >> a >> b; // a contains "Hello," b contains "World!".

Stream-Operatoren für eigene Klassen

Zur Serialisierung und Deserialisierung

#include <iosfwd> // Forward declarations for istream and ostream.

class Foo {
	int bar;
	friend std::istream & operator>> (std::istream &,       Foo &);
	friend std::ostream & operator<< (std::ostream &, const Foo &);
};

#include "Foo.hh"

std::istream & operator>> (std::istream & is, Foo & foo)
{
	return is >> foo.bar;
}

std::ostream & operator<< (std::ostream & os, const Foo & foo)
{
	return os << foo.bar << '\n';
}

Stream aus Datei-Deskriptor erzeugen

Achtung: Dies ist nicht Standard-konform und höchst Plattform-abhängig!

#include <fcntl.h>
#include <istream>
#include <streambuf>

int fd = ::open ("blafasel.txt", O_RDONLY);
std::streambuf * sb = new __gnu_cxx::stdio_filebuf<char> (fd, std::ios::in);
std::istream is (sb);