Logistics Project IOStreams II Part 2 (water) due Sunday, Oct 16 - - PDF document

logistics
SMART_READER_LITE
LIVE PREVIEW

Logistics Project IOStreams II Part 2 (water) due Sunday, Oct 16 - - PDF document

Logistics Project IOStreams II Part 2 (water) due Sunday, Oct 16 th Feedback by Monday Part 3 (block) due Sunday, Oct 30 Questions? Exam Logistics Exam 2 Final exam Thursday, October 27 Good newsbad


slide-1
SLIDE 1

1

IOStreams II Logistics

  • Project

– Part 2 (water) due Sunday, Oct 16th

  • Feedback by Monday

– Part 3 (block) due Sunday, Oct 30

  • Questions?

Exam

  • Exam 2

– Thursday, October 27 – More details on Thursday.

Logistics

  • Final exam

– Good news…bad news – Good news

  • Last day of finals, November 18th

– Bad news

  • 8am-10am

– Room

  • 01-3338

Plan for this week

  • I/O Week

– Yesterday: IOStreams 1 – Today: IOStreams 2 – Thursday: IOStreams 3

IOStreams

  • Suite of classes for performing I/O in C++
  • Reading and Writing:

– Standard input and output – File input and output – Input and Output to strings

slide-2
SLIDE 2

2

Streams

  • Like Java, Basic low level mechanism for I/O is

the stream

– Stream is a sequence of bytes

Streams

  • Unlike Java, the basic stream in C++ is

buffered.

– Used to increase efficiency – When the program writes something, it is put into a buffer in memory. – The output doesn't appear on the screen until the buffer is flushed (written)

IOStream class inheritance

cin is an istream cout and cerr are ostreams Other classes inherit from these and add I/O to/from a device, string, or memory

Today

  • Today we will look at:

– fstream – I/O to/from files – sstream – I/O to and from strings – Writing your own << and >> operators.

IOStream Class Hierarchy fstream

  • Reading and Writing from Files

– ifstream (inherits from istream) for input files – ofstream (inherits from ostream) for

  • utput files

– fstream (inherits from iostream) for files that can support both input and output.

slide-3
SLIDE 3

3

Constructors

  • Default constuctors

– ifstream(), ifstream(), fstream() – Creates an unopened file.

  • Can use open() to attach to a file
  • Construct and Open

– ifstream( const char *name, int mode = ios::in, int perm = 0644 ); – ofstream( const char *name, int mode = ios::out, int perm = 0644 ); – fstream( const char *name, int mode, int perm = 0644 ); – badbit set if open fails

Open / Close

  • open( const char *name, int

mode, int perm = 0644 );

– Opens a file and attaches to a stream

  • close();

– closes the file associated with a stream. The stream can be reopened with another file after this.

Using fstreams

  • Since fstreams are derived from istream and
  • stream, I/O is the same as using cin and

cout.

Using fstreams

  • fstreams support random access

– istream &istream::seekg( streamoff

  • ffset, ios::seek_dir where );

– ostream &ostream::seekp( streamoff

  • ffset, ios::seek_dir where );
  • ios::seek_dir = { ios::begin,

ios:end, ios::cur};

– these are actually implemented in istream and

  • stream, and can be used on any stream that is

associated with a seekable device

fstreams

  • Questions?

sstreams

  • adds functionality to do “input” and

“output” from/to arrays of characters in memory.

  • no actual I/O is done; however, the

conversions performed are exactly the same as those we have already covered.

  • C++ means of doing atoi(), itoa()
slide-4
SLIDE 4

4

sstreams

  • E.g. commandline arguments

main (int argc, char *argv[]) { int intArg; float floatArg; istringstream ints (argv[1]); istringstream floats (argv[2]); ints >> intArg; floats >> floatArg; … }

sstreams

  • One reason to use an istrstream is to do

conversions on data that have already been read in.

sstreams

  • Example: consider a program whose input is

supposed to contain four values on each line:

1 2 3 4 5 6 7 8 9 10 11 12

– Consider the obvious approach:

  • cin >> a >> b >> c >> d;
  • The extractions will skip white space automatically. If the

input is erroneous, for example:

1 2 3 4 5 6 7 9 10 11 12

sstreams

char buffer[BUFLEN]; int a,b,c,d; while( cin.getline( buffer, BUFLEN ) ) { istringstream S (buffer); S >> a >> b >> c >> d; … }

sstreams

  • E.g. itoa (toString)

char buffer[20];

  • stringstream outs (buffer);

int i = 20;

  • uts << I;

sstreams

  • Questions?
slide-5
SLIDE 5

5

IOStream Class Hierarchy

Insertion and Extraction

  • Inserters and extractors for built-in

datatypes (like int, double, float, etc.) are predefined member functions of the IOStream classes.

  • It would be nice if we can do I/O on objects
  • f our own classes in the same manner as

the basic datatypes. Insertion and Extraction int i=5; double d = 7.0; myClass foo (7); cout << i << d << foo;

Operator overloading

  • All C++ operators can be overloaded on a

class by class basis.

  • Overloaded operators call specially named

class methods.

– Keyword operator followed by operator to be overloaded. – E.g

  • operator+

Operator overloading

  • Overloaded operators can also be defined

globally as non-members (outside of the class definition)

Operator overloading

  • Friends

– By declaring a function as a friend, we allow it access to a class’s private data members (both data and methods)

slide-6
SLIDE 6

6

Operator overloading

  • Global operator definitions

friend Complex operator+( Complex &c1, Complex &c2 ); friend Complex operator-(Complex &c1); friend bool operator== (const Complex &c1, const Complex &c2); friend Complex& operator+= (Complex &c1, const Complex &c2); friend Complex& operator+= (Complex &c, double d);

Insertion and Extraction – writing your own

  • Recall how operators work:

Point P (0,0); cout << P;

is the same as:

cout.operator<< (P);

however, who can predict the need for an

  • perator for each class?

Insertion and Extraction – writing your own

  • Instead, the compiler looks for:

– operator<<( cout, P );

  • Note that this can’t be a member of Point

since cout is the first argument.

  • Take home message: operator<< and
  • perator>> are defined outside of a

class.

Writing an inserter (operator<<)

1. The first argument should be a reference to an

  • stream. The second argument should be a constant

reference to your class. 2. The function should return a reference to an ostream so that insertions can be chained. 3. The body of the function should perform whatever

  • utput is appropriate for your class, but nothing more!

4. If you need to access the private data members of your class directly, then your class must declare this function to be a friend

Writing an inserter (operator<<)

friend ostream &operator<<( ostream &out, const Point &p ) {

  • ut << ’(’ << p.x << ’,’ << p.y << ’)’;

return out; }

Output:

(0,0)

Writing an extractor (operator>>)

  • Like inserter except:

– Must handle possible errors – Argument cannot be const reference. – Almost surely will have to declare as a friend.

slide-7
SLIDE 7

7

Writing an extractor (operator>>)

friend istream &operator>> (istream &in, Point &p) { char c; int ok = FALSE; in >> c; if (c == ‘(‘)) { in >> p.x >> c; if (c == ‘,’) { in >> p.y >> c; if (c == ‘)’) ok = TRUE; } } if (!ok) in.clear (in.rdstate() | ios::failbit return in; }

Writing an extractor (operator>>)

  • Things to note:

– Checks to see if in same format as output. – Stops reading as soon as an error is found. – Sets failbit if format is not correct.

  • Questions?

Summary

  • fstream – I/O to/from files
  • sstream – I/O to/from strings (char arrays)
  • Rolling your own extractors and inserters.
  • Questions?