io streams iostreams are part of the standard c++ library meant to be used in place of c stdio...

31
Copyright 2006 Oxford Consulting, Ltd 1 February 2006 - 1 - Exploring the C++ Stream Library Exploring the C++ Stream Library IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Upload: tadhg

Post on 25-Feb-2016

37 views

Category:

Documents


3 download

DESCRIPTION

IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library. IO Streams Considerations for IOStreams vs. printf... printf uses format strings - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 1 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

IO Streams

IOStreams are part of the Standard C++ library

Meant to be used in place of C stdio library

So why re-invent the standard io library

Page 2: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 2 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

IO Streams

Considerations for IOStreams vs. printf... printf uses format strings

printf format strings are interpreted at run time not

compile time

With printf binding between variables and values occurs

at runtime

With IO streams there are no format strings

Cannot overload printf

Page 3: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 3 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

IOStream Class HierarchyIOS

streambuf* readbuf( )

ostreamconst ostream& operator << (type)

const ostream& operator << (streambuf*)

istrstreamifstreamofstream ostrstream

istreamconst istream& operator >> (type)

const istream& operator >> (streambuf*)

streambuf

Page 4: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 4 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

Input StreamsThe extraction Operator >>

Performs the same function as scanf in the C stdio library

Uses white space to delimit its arguments

Type at the keyboard234 678

Execute codeint i, j;cin >> i >> j;

i contains 234j contains 678

Page 5: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 5 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

Input StreamsThe extraction Operator >>

Because istream extraction works like scanf….. It has many of the same problems

Type at the keyboardmy name

Execute codeint i, j;cin >> i >> j;

values I and j are left unchanged

Page 6: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 6 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

Input Streams

istream member functions to the rescue….

get( )

getline( )

Allow us to read an entire line or buffer

Page 7: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 7 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

Input Streams

get Member Function Prototypes

class istream{public:

...istream& get(char* ptr, int len, char delim = `\n');istream& get(char& c);int get();istream& getline(char* ptr, int len, char delim = `\

n');istream& getline(char& c);int getline();istream& read(char* ptr, int n);

};

Page 8: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 8 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

Input Streams

Member function get

Leaves delimiter in input stream

Member function getline

Reads and discards delimiter

Member function read

Used to read raw non-delimited

bytes

Page 9: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 9 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

Output StreamsThe Insertion Operator <<

Performs the same function as printf in the C stdio library Its function members are similar to those in the istream class

class ostream{

public:...ostream& put(char c);ostream& write(const char* buffer, int length);

};

Page 10: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 10 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

Output StreamsThe Insertion Operator <<

Member Function putInserts single character into output stream

Member Function writeWrites specified number of characters ...From designated buffer into output stream

Page 11: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 11 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

Error Handling on InputReading Correct Input Data...

IOstreams designed so they can be tested

if (cin)if (!cin)

Return true If the last read was successful

Return false If the last read was unsuccessful

Page 12: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 12 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

char buffer[256];while (cin.getline(myBuff, sizeof(myBuf))) { // process the characters in the buffer.}

char buffer[256];while (cin.get(myBuff, sizeof(myBuf))) { cin.get() // read and discard newline // process the characters in the buffer.}

Error Handling on Input

Page 13: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 13 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

Error Handling on Input

Testing for Errors… cin.good()

Returns true if no errors

cin.eof()Returns true if the input stream is at end of file

cin.fail() Returns true if some input operation has failed

cin.bad()Returns true if something has gone wrong with

streambuf

Page 14: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 14 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

Error Handling on Input

Testing for Errors…

Once an input stream returns an error, it will no longer read any input

To clear an error condition on a stream one must call the clear() error member function

Syntaxcin.clear();

Page 15: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 15 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

File IO Streams

ifstream and ofstreamTo open file…

File stream's constructor is passed the file to open

Prototypeofstream output( char* myFileName );

Page 16: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 16 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

File IO Streams

ifstream and ofstream

void main( void ){ ofstream output("myFile.dat"); if (!output) { cerr << "Oops problem!!! Can’t open myFile.dat" << endl; } return;}

Page 17: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 17 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

File IO Streams

ifstream and ofstreamObserve the function ignore( ) ….

It’s tossing out the newline character left in input stream by

the get function

Prototypeistream& ignore(int n = 1, int delim = EOF);

n specifies the number of characters to ignore

delim character after which ignore quits.

Page 18: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 18 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

File IO Streams

ifstream and ofstream

Observe the second argument to the ifstream constructor

ios::inSpecifies file to be opened for input

Default is input on an input stream

Page 19: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 19 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

IO StreamsSeeking in IOStreams...

Absolute stream location in a stream called streampos

Can obtain current stream position tellp( ) member function of ostream

Identifies the current position in the output streambuf

tellg( ) member function of istream Identifies the current position in the

input streambuf

To move within a streambuf seekp member function of ostream seekg member function of istream

Page 20: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 20 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

IO Streams

Seeking in IOStreams...Prototypes

seekp (position); // absolute positionseekp (offset, ios::seekdir); // relative position

seekg (position); // absolute positionseekg (offset, ios::seekdir); // relative position

seekdir beg - seek from beginning of current filecurr - seek from current positionend - seek backwards from end of current file

Page 21: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 21 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

StrstreamsFormatting Using Streams…

Often when working with display screens necessary to dynamically create messages

Useful functions in the C stdio library for doing such formatting

functions sprintf and sscanf

C++ also provides similar functionality with strstreams.

Must include <strstrea.h>

Page 22: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 22 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

Strstreams

istrstreams…Constructors for istrstream

Create an input stream with the string passed to constructor as input

istrstream::istrstream(char* buf);istrstream::istrstream(char* buf, int size);

Page 23: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 23 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

Strstreamsostrstreams…

Constructors for ostrstream

Create an output stream with the string passed to

constructor as input

ostrstream::ostrstream(char* buf, int size, int = ios::out);out - characters formatted into starting address of buffer

Write pointer positioned at start of bufferapp - characters appended to end of buffer

Write pointer positioned at null terminator at end of string

Page 24: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 24 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

Strstreams

ostrstreams…

The ostrstream will only allow size characters to be inserted

ostrstream does not automatically insert a null terminator into buffer….Special manipulator ends provided

Page 25: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 25 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

Strstreams

ostrstreams and Storage Allocation User Specified

char myBuffer[128];ostrstream myString(myBuffer, sizeof(myBuffer));myString << "Bonjour la Monde" << ends;

Automatic

ostrstream myString;// myString automatically allocates storage to hold the string.myString << " Bonjour la Monde” << ends;;

Page 26: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 26 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

Strstreams

Accessing ostrstream Contents...

To access the string an ostrstream contains

Use member function str( ) …..

….Returns a pointer to internal buffer holding the stringchar* cp = A.str();

Page 27: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 27 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

Strstreams

Accessing ostrstream Contents and Freezing the Stream

Invoking member function str ( ) freezes the stream

Action causes ostrstream return pointer to its internal buffer

User invoking str ( ) is now responsible for managing

the storage

Page 28: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 28 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

Strstreams

Accessing ostrstream Contents and Unfreezing the Stream

Frozen stream can be unfrozen ... returns pointer to ostrstream

Syntax

ostrstreamInstance.rdbuff()->freeze(int arg = 1);arg – 0 Unfreeze the streamarg – 1 (or none) Freeze the stream

Page 29: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 29 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

IO Streams

Overloading the Insertion Operator

When working with streams a particularly useful operator to

overload is insertion operator

Can specify how to send data to standard out or to a file

Such ability can also be useful for debugging or saving the

state of a program

Page 30: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 30 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

IO Streams

Overloading the Insertion or Extraction Operators

When overloading insertion or extraction operator

Must support constructs of form

cout << A << B << C…cin >> J >> L >> M…

Page 31: IO Streams IOStreams are part of the Standard C++ library Meant to be used in place of C stdio library So why re-invent the standard io library

Copyright 2006 Oxford Consulting, Ltd 1 February 2006

- 31 -

Exploring the C++ Stream LibraryExploring the C++ Stream Library

IO Streams

Overloading the Insertion or Extraction Operators

Syntaxostream& operator<<(ostream& streamObject, const type& instance); streamObject - instance of a steam object passed by

reference instance - instance of a user defined variable

passed by const reference