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
public:
typedef typename Traits::int_type int_type;
typedef typename Traits::pos_type pos_type;
typedef typename Traits::off_type off_type;
virtual ~streambuf ();
locale getloc () const;
locale pubimbue (const locale & loc);
streambuf * pubsetbuf (char * s, streamsize n);
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 ();
int_type sgetc ();
streamsize sgetn (char * s, streamsize n);
int_type sputbackc (char c);
int_type sungetc ();
int_type snextc ();
int_type sputc (char c);
streamsize sputn (const char * s, streamsize n);
int pubsync ();
protected:
streambuf ();
void setg (char * gbeg_arg, char * gnext_arg, char * gend_arg);
char * eback () const;
char * gptr () const;
char * egptr () const;
void setp (char *, char *);
char * pbase () const;
char * pptr () const;
char * epptr () const;
void gbump (int n);
void pbump (int n);
virtual void imbue (const locale & loc);
virtual streambuf * setbuf (char * s, streamsize n);
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 ();
virtual streamsize xsgetn (char * s, streamsize n);
virtual int_type underflow ();
virtual int_type uflow ();
virtual int_type pbackfail (int_type = Traits::eof ());
virtual streamsize xsputn (const char * s, streamsize n);
virtual int_type overflow (int_type eof = Traits::eof ();
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 };
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;
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 ();
void init (streambuf * sb);
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"
std::flush
std::endl
std::ends
std::skipws std::noskipws ios ::skipws
ios ::unitbuf
std::boolalpha std::noboolalpha ios ::boolalpha
std::showpos std::noshowpos ios ::showpos
std::dec ios ::dec
std::hex ios ::hex
std::oct ios ::oct
std::showbase std::noshowbase ios ::showbase
std::showpoint std::noshowpoint ios ::showpoint
std::fixed ios ::fixed
std::scientific ios ::scientific
std::uppercase std::nouppercase ios ::uppercase
std::right ios ::right
std::left ios ::left
std::internal ios ::internal
std::setw (n) std::setw (0) ostream .width (n)
std::setfill (c) std::setfill (' ') ostream .fill (c)
std::setprecision (n) ostream .precision (n)
std::setbase (b)
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
Kombinierter Ein-/Ausgabestrom
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
String Streams
String Streams implementieren die Schnittstelle für
Datenströme auf der Basis speicherbasierter Puffer.
#include <sstream>
String buffer
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
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);