copyright © 2012 pearson education, inc. chapter 5 working with data files

42
Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Upload: brandon-warren

Post on 21-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Copyright © 2012 Pearson Education, Inc.

Chapter 5

Working with Data Files

Page 2: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Copyright © 2012 Pearson Education, Inc.

OutlineObjectives

1. Defining File Streams

2. Reading Data Files

3. Generating a Data File

4. Problem Solving Applied: Data Filters – Modifying an HTML File

5. Error Checking

6. Numerical Technique: Linear Modeling*

7. Problem Solving Applied: Ozone Measurements*

Page 3: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Copyright © 2012 Pearson Education, Inc.

ObjectivesDevelop problem solutions in C++ that:

• Open and close data files for input and output.

• Read data from files using common looping structures.

• Check the state of an input stream.

• Recover from input stream errors.

• Apply the numerical technique of linear modeling.

Page 4: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Defining File Streams

Copyright © 2012 Pearson Education, Inc.

Page 5: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Standard Input and Output

• C++ defines the cin and cout objectsto represent the standard input (keyboard) and standard output (console).

• C++ also defines the standard error stream, cerr.

• Standard input is a kind of general input stream class; standard output and error streams are special kinds of general output stream.

Copyright © 2012 Pearson Education, Inc.

Page 6: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Other kinds of Streams

• Standard C++ library also definesspecial kinds of streams for file input and output.– #include <ifstream>– #include <ofstream>

Copyright © 2012 Pearson Education, Inc.

Page 7: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Stream Object Hierarchy

Copyright © 2012 Pearson Education, Inc.

Page 8: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

The ifstream Class• The ifstream (input file

stream) class is derived from istream (input stream class).– Thus ifstream inherits the

input operator and member functions eof() and fail() defined in istream.

– The ifstream class also defined specialized methods specific to working with files.

Copyright © 2012 Pearson Education, Inc.

Page 9: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Input File Streams

• Each data files used for input musthave an ifstream object associated with it:

ifstream sensor1;sensor1.open(“sensor1.dat”);

- Or just –

ifstream sensor1(“sensor1.dat”);Copyright © 2012 Pearson Education, Inc.

Page 10: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Avoiding Bugs

• If opening the named file fails, the failerror bit is set, and all statements to read from the file will be ignored.– NO error message will be generated but the

program will continue to execute.– Check to be sure that the open was

successful.• fail() method of istream returns false.

Copyright © 2012 Pearson Education, Inc.

Page 11: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Input File Example

ifstream sensor1;sensor1.open("sensor1.dat");if ( sensor1.fail() ) //open failed{ cerr << "File sensor1.dat could not be opened"; exit(1); //end execution of the program}

Copyright © 2012 Pearson Education, Inc.

Page 12: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

The ofstream Class• The ofstream (output file

stream) class is derived from ostream (output stream class).– Thus ofstream inherits the

output operator and member functions ostream.

– The ofstream class also defined specialized methods specific to working with files.

Copyright © 2012 Pearson Education, Inc.

Page 13: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Output File Streams

• Each data files used for output musthave an ofstream object associated with it:

ofstream sensor1;sensor1.open(“balloon.dat”);

- Or just –

ofstream sensor1(“balloon.dat”);Copyright © 2012 Pearson Education, Inc.

Page 14: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Output File Modes

• By default, opening a file for outputin this way will either create the file if it doesn’t already exist or overwrite a previously existing file.

• If you wish to simply append new content to the previously existing file, you may open the file in append mode:

sensor1.open(“balloon.dat”, ios::append);

Copyright © 2012 Pearson Education, Inc.

Page 15: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

More on File Objects

• The close() methods for both inputand output stream objects should be called when the program is done with the file.– The method(s) will be called automatically when

the program exits.

• When you use a string class to represent the file name, you must use the c_str() method of string to provide the appropriate type for use in input and output open/constructor methods.

Copyright © 2012 Pearson Education, Inc.

Page 16: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Reading Data Files

Copyright © 2012 Pearson Education, Inc.

Page 17: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

File Formats

• To read a file, some knowledge aboutthe contents of the file are needed:– Name of the file.– Order and data types of the values stored in the file.

• Three common structures:– First-line in file contains number of lines/records in the

file.– Trailer signal/sentinel signal used to indicate the last

line/record in the file.– End-of-file used directly to indicate last line/record in

the file.Copyright © 2012 Pearson Education, Inc.

Page 18: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Specified Number of Lines

sensor1.dat100.0 132.50.1 147.20.2 148.30.3 157.30.4 163.20.5 158.20.6 169.30.7 148.20.8 137.6

Copyright © 2012 Pearson Education, Inc.

//open input fileifstream sensor1(“sensor1.dat”);//read the number of data entriesint numEntries;sensor1 >> numEntries;//read every rowdouble t, y;for (int i = 0; I < numEntries; i++) {

sensor1 >> t >> y;//do something with the data

}

Page 19: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Trailer/Sentinel Signal

sensor2.dat0.0 132.50.1 147.20.2 148.30.3 157.30.4 163.20.5 158.20.6 169.30.7 148.20.8 137.6-99 -99

Copyright © 2012 Pearson Education, Inc.

//open input fileifstream sensor2(“sensor2.dat”);//read every rowdouble t, y;do {

sensor1 >> t >> y;if (t < 0 || y < 0) break;//do something with the data

} while (! sensor1.eof());

Page 20: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

EOF-based File Input

sensor3.dat0.0 132.50.1 147.20.2 148.30.3 157.30.4 163.20.5 158.20.6 169.30.7 148.20.8 137.6

Copyright © 2012 Pearson Education, Inc.

//open input fileifstream sensor3(“sensor3.dat”);//read every rowdouble t, y;while (! sensor3.eof()) {

sensor1 >> t >> y;if (sensor3.fail()) break;//do something with the data

}

Page 21: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Generating a Data File

Copyright © 2012 Pearson Education, Inc.

Page 22: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Writing Files

• After opening an output file, writing toa file is no different from writing to standard output.

• Must decide on a file format.– Sentinels can be hard to choose to avoid conflict with

valid data.– Knowing in advance how many lines/records are in

the file is sometimes difficult or impractical.

– Usually best to use eof-style file format where file structure contains only valid information.

Copyright © 2012 Pearson Education, Inc.

Page 23: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Problem Solving Applied: Data Filters – Modifying

an HTML File

Copyright © 2012 Pearson Education, Inc.

Page 24: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Problem Solving Applied: Data Filters – Modifying

an HTML File

Copyright © 2012 Pearson Education, Inc.

Page 25: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Problem Solving Applied: Data Filters – Modifying

an HTML File

Copyright © 2012 Pearson Education, Inc.

Page 26: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Problem Solving Applied: Data Filters – Modifying

an HTML File

Copyright © 2012 Pearson Education, Inc.

Page 27: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Problem Solving Applied: Data Filters – Modifying

an HTML File

Copyright © 2012 Pearson Education, Inc.

Page 28: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Error Checking

Copyright © 2012 Pearson Education, Inc.

Page 29: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Error Checking

• In addition to indicating if an erroroccurs when opening a file, fail() may also be used to detect other types of errors.– Errors can occur when trying to read from an

input stream when the data on the input stream isn’t of the appropriate type.

Copyright © 2012 Pearson Education, Inc.

Page 30: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Whitespace

• Whitespace is defined as blank, tab, newline, form feed, and carriage return characters.

• Used to separate data when reading from an input stream.

• For example, to read 2 integers from an input stream, there must be whitespace between the two numbers.– C++ ignores the whitespace to read the numbers.– If any other characters are encountered, the error (fail)

state is set.

Copyright © 2012 Pearson Education, Inc.

Page 31: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Input Stream Errors

• Do not generate notification of errors!– Only set the error state.

• Reads will not affect the state of the input buffer or alter variables.– However the program will continue executing!

• Must “clear” the error state before additional input may be read (using istream’s clear() method).

Copyright © 2012 Pearson Education, Inc.

Page 32: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Stream State

• Indicated by a set of state flags:– badbit, failbit, eofbit, goodbit.

• Events may alter the stream state:

Copyright © 2012 Pearson Education, Inc.

Event badbit failbit eofbit goodbit

Initialization of a stream

0 0 0 1

Failure to open a file 0 1 0 0

Unexpected data encountered

0 1 0 0

End of file encountered

0 1 1 0

Page 33: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Stream Class Methods

Copyright © 2012 Pearson Education, Inc.

Method Description

bool bad() Returns true iff badbit is set.

bool eof() Returns true iff eofbit is set.

bool fail() Returns true iff failbit is set.

bool good() Returns true iff goodbit is set.

void clear(iostate flag = goodbit) Sets the state flags.

iostate rdstate() Returns the value of the state flags.

Page 34: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Numerical Technique: Linear Modeling*

Copyright © 2012 Pearson Education, Inc.

Page 35: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Linear Modeling

• Linear modeling is the name given tothe process that determines the linear equation (y = mx + b) that is the best fit to a set of data points.– Linear regression does this by minimizing the

squared distance between the line and the data points.

Copyright © 2012 Pearson Education, Inc.

Page 36: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Example

Copyright © 2012 Pearson Education, Inc.

Page 37: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Solving for Linear Model Parameters

Copyright © 2012 Pearson Education, Inc.

Page 38: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Problem Solving Applied: Ozone Measurements*

Copyright © 2012 Pearson Education, Inc.

Page 39: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Problem Solving Applied: Ozone Measurements*

Copyright © 2012 Pearson Education, Inc.

Page 40: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Problem Solving Applied: Ozone Measurements*

Copyright © 2012 Pearson Education, Inc.

Page 41: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Problem Solving Applied: Ozone Measurements*

Copyright © 2012 Pearson Education, Inc.

Page 42: Copyright © 2012 Pearson Education, Inc. Chapter 5 Working with Data Files

Problem Solving Applied: Ozone Measurements*

Copyright © 2012 Pearson Education, Inc.