+ All Categories
Home > Documents > Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Date post: 18-Jan-2018
Category:
Upload: gerald-murphy
View: 220 times
Download: 0 times
Share this document with a friend
Description:
Conceptually, there is little difference between a class in C++ and a class in C#. However, there is a significant difference in the structure of a program that contains class definitions.
67
Introduction to C++ Part III Version 1.0
Transcript
Page 1: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Introduction to C++

Part IIIVersion 1.0

Page 2: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

TopicsC++ ClassesFile I/O

Page 3: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Conceptually, there is little differencebetween a class in C++ and a class in

C#. However, there is a significant difference in the structure of a program

that contains class definitions.

Page 4: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Consider the followingClass diagram …

Rectangle

- width: int- length: int

+ Rectangle(int, int)+ getWidth( ): int+ getLength( ): int

Page 5: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

In C# we might write

public class Rectangle{ private double width; private double length;

public double getWidth( ) { return width; }

public double getLength( ) { return length; }}

Page 6: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

In C++ it is common to write a class like thisin two distinct files

- A .h file that contains the class definition

- A .cpp file that contains the implementation

Page 7: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Class Rectangle { private: int width; int length; public: Rectangle(int, int); int getWidth( ); int getLength( ); };

The .h file

Notice the semicolon

Page 8: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

#include rectangle.h

Rectangle::Rectangle(int p1, int p2) { width = p1; length = p2; }

int Rectangle::getWidth( ) { return width; }

int Rectangle::getLength( ) { return length; }

The .cpp file

include the .h file

name the class

Page 9: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

public class Rectangle{ private double width; private double length;

public double getWidth( ) { return width; }

public double getLength( ) { return length; }}

class Rectangle { private: int width; int length; public: Rectangle(int, int); int getWidth( ); int getLength( ); };

#include rectangle.h Rectangle::Rectangle(int p1, int p2) { width = p1; length = p2; } int Rectangle::getWidth( ) { return width; } int Rectangle::getLength( ) { return length; }

In C++ we would write

In C# we might write

Page 10: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

You can put all of the code in one file, but DON’Tas this is inline code and can cause many problems!

Class Rectangle { private: int width; int length; public: Rectangle(int p1, int p2)

{ width = p1; lenth = p2;

} int getWidth( ) { return width ;

} int getLength( )

{ return length;}

};

Page 11: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

A Driver

#include <iostream>#include “rectangle.h”using namespace std;

int main( ){ Rectangle joe(4, 5); cout << joe.getWidth( ); . . .}

Standard .h files

Your .h files

Page 12: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

The following code createsa Rectangle object using anon-parameterized constructor.

Rectangle joe;

Don’t do this!

Rectangle joe( );

Page 13: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

If you don’t write any constructors, thecompiler will create a default, non-parameterized constructor for you.

If you write a parameterized constructor,the compiler does not create a defaultnon-parameterized one.

Page 14: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

When an object is created like this

Employee joe;

the employee object is stored on the stack. It is a local variable.

Page 15: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Creating a “DRIVER” class

In a well designed Object Oriented Program themain( ) function does very little. It’s job is to create the objects necessary to run the programand then it lets them do the work. The objects inthe program communicate by passing messages toone another.

Page 16: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Think of the C# model which places Maininside of the static class “Program”

Page 17: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

To illustrate let’s write a program that simulates the rolling of a pairof dice.

We will create two classes * one represents a pair of dice * the other is our “driver”

Page 18: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

The Dice class …

class Dice{ public:

Dice(void);void roll( );int getDieOne( ) const;int getDieTwo( ) const;

private:int dieOne; // value of die oneint dieTwo; // value of die two

};

Objects of the Dice class represent a pair of dice.

Page 19: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

class Driver{ public:

Driver(void);char getYN( ) const;void run( );void output(int, int);

};

The driver class manages any console I/O and “runs” the program.

Page 20: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Here is the run( ) function in theDriver class. Note how it creates a Dice object. The code here is very similarto what you might think to put in main( )

void Driver::run( ){ Dice luckyEddies; char key;

cout << "\nLucky Eddie's Dice Game of Chance ...."; do { key = getYN( ); if (key == 'y') { luckyEddies.roll( ); output (luckyEddies.getDieOne( ), luckyEddies.getDieTwo( ) ); } }while (key == 'y');}

Page 21: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Then main( ) simply looks like this:

int main( ){ Driver diceProgram; diceProgram.run( ); return 0;}

Page 22: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

File I/O in C++

Page 23: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Stream Objects

cin – the standard input stream - an object of the istream class,

cout – the standard output stream – an object of the ostream class

These streams are automatically created for you whenyour program executes. To use them you only need to#include <iostream> and the appropriate using directives.

You have been introduced to these stream objects …

Page 24: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

To do file I/O, we will use a newset of stream classes

ifstream – objects of this class represent file input

ofstream – objects of this class represent file output

Page 25: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

File I/O

When a program takes input from a file, we say thatit reads from the file.

When a program puts data into a file, we say that it writes to the file.

To read or write to a file, we create a stream object, and connect it to the file.

Page 26: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Text FilesData in a file can either be text or binary.

Everything in a text file appears as readablecharacters. You can look at the file with a texteditor, such as notepad.

Text files are sometimes referred to as Formatted files.

This semester we will only deal with text files.

Page 27: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

The fstream classes

We use objects of the ifstream class to read from a file, and objects of the ofstream class to write toa file.

These classes are defined in <fstream>. To use themwe must write

#include <fstream>

Page 28: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

ifstream Operations

>> stream extraction operatorget ( ) extract one charactergetline( ) read a line of data into a stringignore( ) skip a character

these ought to look familiar, they are the samefunctions we use on standard In and standard Out.

Page 29: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

ifstream fileData (“c:\\thefile.txt”);

char a;string s;int n;

a = fileData.get( );fileData.ignore( ); // pass over newlinegetline(fileData, s);fileData >> n;

Example Code

Page 30: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Declaring A Stream

To use a stream object, it must first be declared

ifstream inStream;ofstream outStream;

Page 31: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Stream variables

A stream object is like any other variable, butthere are some important exceptions:

You cannot assign a value to a stream variable

If you pass a stream variable as a parameter, youmust pass it by reference.

Page 32: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Connecting a Stream to a File

ifstream inputStream;inputStream.open (“theData.txt”);

Widget 123V89001 12.95theData.txt

inputStream

to program

you can declare and connect to a file at the same time:

ifstream inputStream (“theData.txt”);

Page 33: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Paths

ifstream inputStream;inputStream.open (“theData.txt”);

if no path is specified, the file is assumedto be in the same directory as the programthat is executing.

When you code a \ in a pathname, you mustwrite \\. Why?

inputStream.open (“c:\\theData.txt”);

Page 34: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Reading From a Text FileTo read data from a text file, use the streamextraction operator, just as you would use it to readfrom the keyboard.

string description; // no spacesstring partNumber; // no spacesdouble price;

ifstream inputStream;inputStream.open (“theData.txt”);…inputStream >> description >> partNumber >> price;…

Page 35: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Reading From a Text File

If the data contains spaces, e.g. a person’s namethen you must use getline.

string name;

ifstream inputStream;inputStream.open (“theData.txt”);…getline(inputStream, name);…

Page 36: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Type Safe I/OIn the C programming language, input and outputare done with very different libraries than thoseused in C++ (Although you can still use the C I/Olibraries in C++ if you want to).

One of the advantages to using the new C++ librariesis that C++ I/O is typesafe. The << and >> operators areoverloaded to do the correct thing for all of the standarddata types.

These operators can also be overloaded for user defineddata types.

Page 37: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Widget 123V89001 12.95theData.txt

inputStream

Data is read from the file and converted intothe appropriate data type.

double price;inputStream >> price;

in the file, this ischaracter data.

price

convert fromcharacter to double

12.95

…111000011010…

Page 38: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Writing to a Text File

Use the stream insertion operator to write to a text file,just as if you were writing to the display.

ofstream outputStream;outputStream.open (“theData.txt”);

outputStream << price;

Page 39: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Opening an Output file

If the named file does not exist, the file is created.

If the named file already exists, it is opened, andthe contents of the file are discarded, by default.

Page 40: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Formatting the Output

Most of the time, when we write data to a file, it iswith the idea in mind that the data will be read infrom this or some other program.

It is up to the programmer to format the data in theoutput file, so that it can later be read in a meaningfulway.

Page 41: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Example

int a = 5;int b = 15;int c = 239;

ofstream myOutputStream (“mydata.txt”);myOutputStream << a << b << c;

515239

What happens when you try to read this file?

Page 42: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

int a = 5;int b = 15;int c = 239;

ofstream myOutputStream (“mydata.txt”);myOutputStream << a << “ “ << b << “ “ << c;

add white space toseparate the fields!

Page 43: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Closing the File

It is good programming practice to close a fileas soon as you are done using it.

myOutputStream.close ( );

Although a file is closed automatically when aprogram ends normally, it is not closed if an erroroccurs and the program terminates abnormally.If the file is not closed, all of the data written tothe file is lost!

Page 44: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Mode Bits

When a file is opened, you can supply bits that furtherdefine the file. These mode bits are defined in the iosclass. The ios class contains a number of importantconstants we use in file I/O.

ios::in open the file for reading, the default for ifstreamios::out open the file for writing, the default for ofstreamios::app open the file for appending. All data is written at the end of the fileios::trunc open the file and discard contents, the default for ofstream

ios::binary open the file for binary content, Note there is no ios::text which is the default

Page 45: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Example

#include <fstream>using std::ofstream;using std::ios;

ofstream myOutputStream (“TheData.txt”, ios::app);

Page 46: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Stream States

Objects of all of the stream classes have a state thatexpresses the condition of the stream..

The stream classes provide functions to query thestream’s state.

good( ) Everything’s fineeof( ) An input operation tried to read beyond the end of the filefail( ) An input operation failed to read the expected character, or an output operation failed to generate the desired charactersbad( ) Indicates the loss of integrity of the underlying input or output sequence

Page 47: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Checking that a File Opened

In C++, errors are not reported unless the programmerexplicitly asks. For example, your program could callfor a file to be opened and then read data from the file.If the file does not exist, no error is reported and youthink that everything worked fine!

Page 48: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

ifstream myInputStream;myInputStream.open (“someData.txt”);

if (myInputStream.fail( )){ cout << “Could not open file”;}else{ …

check the state of thestream here…

We could also write

if (!myInputStream) {

Page 49: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Because I/O is subject to so many errors,it is good programming practice to check

the state of the stream after every I/O operation!

Page 50: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Writing a loop that reads until end of file

Page 51: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

try to open the file

did it open?

print an error

message andquit

no

is the endof file flag

set?

yes

no

Page 52: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

is the eofflag set

?

read a piece of data

was theread good

?

no

process the data

yes

continue with the rest of your program

yes

was the eofflag set

?

no

yes

display an errormessage and quit

no

Page 53: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

int theData;istream myFile;

myFile.open (“someData.txt”);

while (!myFile.eof( )){

Start the read loop

Page 54: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

int theData;istream myFile;

myFile.open (“someData.txt”);

while (!myFile.eof( )){ myFile >> theData; Read some data

Page 55: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

int theData;istream myFile;

myFile.open (“someData.txt”);

while (!myFile.eof( )){ myFile >> theData; if ( myFile.good( ) ) { // process the data }

Test the stream state -- only process the data if the read is good

Page 56: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

int theData;istream myFile;

myFile.open (“someData.txt”);

while (!myFile.eof( )){ myFile >> theData; if ( myFile.good( ) ) { // process the data } else if ( ! myFile.eof( ) ) { // print error message and quit }

If the read fails, testfor end of file

Page 57: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

int theData;istream myFile;

myFile.open (“someData.txt”);

while (!myFile.eof( )){ myFile >> theData; if ( myFile.good( ) ) { // process the data } else if ( ! myFile.eof( ) ) { // print error message and quit }} // end of the read loop

Page 58: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

We can read and test the state of the stream usingthe expression

while (myFile >> theData)

Page 59: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

theFile.open("c:\\theFile02.txt");

while (theFile >> theData){ cout << "\nSuccessfully read in the value " << theData;}

// dropped out, so read failedif (!theFile.eof( ) ){ cout << "\nCould not read an integer - bad data in the file.";}

else{ cout << "\nReached the end of the file.";}

Page 60: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

File Names as Input

char fileName[80];ifstream myData;cout << “Enter in a file name: “;cin >> fileName;myData.open (fileName);…

the filename must be storedin an array of characters. Wewill talk about char arraysin a later section…

Alternatively you could writestring fileName;cin >> fileName;myData.open(fileName.c_str( ) );

Page 61: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Formatting FlagsFormatting flags are defined in the ios class.Stream classes have a member function,setf( flag ) that sets these formatting flags.

left left align output not setright right align output setdec output as decimal sethex output as hexadecimal not setoct output as octal (base 8) not setshowpoint show decimal point on output not setscientific output in exponential format not setfixed output in fixed format (not scientific) not set

Page 62: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

You can combine flags with the | operatormyStream.setf (ios::fixed | ios::showpoint);

A flag remains set until it is unset.myStream.unsetf (ios::fixed);

Page 63: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Stream ManipulatorsStream manipulators go directly into the stream.They are inserted using the stream insertion operator

dechexoctendlsetw (w)setprecision (n);setiosflags (flags);setfill (fillChar);

myStream << setw (5) << theData;

Page 64: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Side Effects

You don’t want a function to have an unwanted side effect.One example would be setting I/O flags in a function andleaving them that way when you exit the function.

The function flags returns a long data type that contains the settings of all of the I/O flags.

long myFlags = myStream.flags ( );…

myStream.flags (myFlags);

without an argument the function returns the flags.

with an argument, the flags are restored to those set in the parameter.

Page 65: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

File Pointers

Streams have pointers associated with them

get pointer points to the place next character will be read from put pointer points to where the next character will be written

tellg( ) returns current get pointertellp ( ) returns current put pointer

seekg ( ) positions get pointerseekp ( ) positions put pointer

Page 66: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

seekp (15); move 15 bytes from start of file

seekp (-10, ios::end);move -10 bytes from end of file

seekg (6, ios::cur); move 6 bytes from current position

Page 67: Introduction to C++ Part III Version 1.0. Topics C++ Classes File I/O.

Binary I/OWe will not do any binary I/O in this course.

Data is written to the output device exactly as itIs stored in memory.

Binary I/O is done with the functions * read( ) * write( )

The parameters are the address of the data bufferand the number of bytes to read or write.

The address must be cast to a char*


Recommended