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

Slides:



Advertisements
Similar presentations
Slide: 1 Interra Induction Training Object Oriented Programming in C++ IO Streams Kolkata, July 25, 2005.
Advertisements

1 Engineering Problem Solving With C++ An Object Based Approach Chapter 4 Programming with Data Files.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
Copyright © 2012 Pearson Education, Inc. Chapter 12: Advanced File Operations.
Chapter 5: Loops and Files.
計算機概論實習 Stream Stream: sequence of bytes Input: from device (keyboard, disk drive) to memory Output: from memory to device (monitor, printer,
C++ plus. 2 Goals Some general C++ tips 3 C++ Tips is header file for a library that defines three stream objects Keyboard an istream object named cin.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.
Programming with Data Files Chapter 4. Standard Input Output C++ Program Keyboard input cin Output Screen cout.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Program Input and the Software Design Process ROBERT REAVES.
Input/Output in C++ C++ iostream.h instead of stdio.h Why change? –Input/output routines in iostream can be extended to new types declared by the user.
CSIS 123A Lecture 8 Streams & File IO Glenn Stevenson CSIS 113A MSJC.
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
File I/O ifstreams and ofstreams Sections 11.1 &
Chapter 9 I/O Streams and Data Files
1 Streams In C++, I/O occurs in streams. A stream is a sequence of bytes Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives.
FILE HANDLING IN C++.
Advanced File Operations Chapter File Operations File: a set of data stored on a computer, often on a disk drive Programs can read from, write to.
File Input and Output in C++. Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 12: Advanced File Operations.
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
4. Input/Output Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 C++ Input/Output: Streams The.
1 I/O  C++ has no built-in support for input/output input/output is a library (iostream)  C++ program views input and output as a stream of bytes  Input:
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Basics Outline 21.1Introduction 21.2Streams Iostream Library.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 12 Advanced File Operations.
1 Today’s Objectives  Announcements Turn in Homework 4 Quiz 4 will be on Wednesday, July 19 – It will have questions about inheritance, polymorphism,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Loops and Files. 5.1 The Increment and Decrement Operators.
Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 4 Working with Data Files.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring Fall 2016 Set 10: Input/Output Streams 1 Based on slides created.
Input/Output. Objectives In this chapter you will: Learn what a stream is and examine input and output streams Explore how to use the input stream functions.
Input/Output in C++ C++ iostream.h instead of stdio.h Why change? –Input/output routines in iostream can be extended to new types declared by the user.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
24 4/11/98 CSE 143 Stream I/O [Appendix C]. 25 4/11/98 Input/Output Concepts  Concepts should be review!  New syntax, but same fundamental concepts.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
Advanced File Operations Chapter File Operations File: a set of data stored on a computer, often on a disk drive Programs can read from, write to.
File I/O. Files allow permanent storage of programs and data. ifstream and ofstream objects –For file I/O the inclusion of is required. –Objects for file.
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Eight: Streams Slides by Evan Gallagher.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
ifstreams and ofstreams
What is a File? A file is a collection on information, usually stored on a computer’s disk. Information can be saved to files and then later reused.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
COMP 2710 Software Construction File I/O
Copyright © 2003 Pearson Education, Inc.
File I/O.
Stream States CSCE 121 J. Michael Moore
Lecture 5A File processing Richard Gesick.
Working with Data Files
Programming with Data Files
when need to keep permanently
Chapter 3: Input/Output
Topics Input and Output Streams More Detailed Error Testing
Chapter 12: Advanced File Operations.
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
Reading from and Writing to Files
ifstreams and ofstreams
Stream States CSCE 121 Based on slides created by Carlos Soto.
Reading from and Writing to Files
Presentation transcript:

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

Copyright © 2012 Pearson Education, Inc. Outline Objectives 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*

Copyright © 2012 Pearson Education, Inc. Objectives Develop 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.

Defining File Streams Copyright © 2012 Pearson Education, Inc.

Standard Input and Output C++ defines the cin and cout objects to 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.

Other kinds of Streams Standard C++ library also defines special kinds of streams for file input and output. –#include Copyright © 2012 Pearson Education, Inc.

Stream Object Hierarchy Copyright © 2012 Pearson Education, Inc.

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.

Input File Streams Each data files used for input must have an ifstream object associated with it: ifstream sensor1; sensor1.open(“sensor1.dat”); -Or just – ifstream sensor1(“sensor1.dat”); Copyright © 2012 Pearson Education, Inc.

Avoiding Bugs If opening the named file fails, the fail error 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.

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.

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.

Output File Streams Each data files used for output must have an ofstream object associated with it: ofstream sensor1; sensor1.open(“balloon.dat”); -Or just – ofstream sensor1(“balloon.dat”); Copyright © 2012 Pearson Education, Inc.

Output File Modes By default, opening a file for output in 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.

More on File Objects The close() methods for both input and 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.

Reading Data Files Copyright © 2012 Pearson Education, Inc.

File Formats To read a file, some knowledge about the 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.

Specified Number of Lines sensor1.dat Copyright © 2012 Pearson Education, Inc. //open input file ifstream sensor1(“sensor1.dat”); //read the number of data entries int numEntries; sensor1 >> numEntries; //read every row double t, y; for (int i = 0; I < numEntries; i++) { sensor1 >> t >> y; //do something with the data }

Trailer/Sentinel Signal sensor2.dat Copyright © 2012 Pearson Education, Inc. //open input file ifstream sensor2(“sensor2.dat”); //read every row double t, y; do { sensor1 >> t >> y; if (t < 0 || y < 0) break; //do something with the data } while (! sensor1.eof());

EOF-based File Input sensor3.dat Copyright © 2012 Pearson Education, Inc. //open input file ifstream sensor3(“sensor3.dat”); //read every row double t, y; while (! sensor3.eof()) { sensor1 >> t >> y; if (sensor3.fail()) break; //do something with the data }

Generating a Data File Copyright © 2012 Pearson Education, Inc.

Writing Files After opening an output file, writing to a 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.

Problem Solving Applied: Data Filters – Modifying an HTML File Copyright © 2012 Pearson Education, Inc.

Problem Solving Applied: Data Filters – Modifying an HTML File Copyright © 2012 Pearson Education, Inc.

Problem Solving Applied: Data Filters – Modifying an HTML File Copyright © 2012 Pearson Education, Inc.

Problem Solving Applied: Data Filters – Modifying an HTML File Copyright © 2012 Pearson Education, Inc.

Problem Solving Applied: Data Filters – Modifying an HTML File Copyright © 2012 Pearson Education, Inc.

Error Checking Copyright © 2012 Pearson Education, Inc.

Error Checking In addition to indicating if an error occurs 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.

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.

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.

Stream State Indicated by a set of state flags: –badbit, failbit, eofbit, goodbit. Events may alter the stream state: Copyright © 2012 Pearson Education, Inc. Eventbadbitfailbiteofbitgoodbit Initialization of a stream 0001 Failure to open a file0100 Unexpected data encountered 0100 End of file encountered 0110

Stream Class Methods Copyright © 2012 Pearson Education, Inc. MethodDescription 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.

Numerical Technique: Linear Modeling* Copyright © 2012 Pearson Education, Inc.

Linear Modeling Linear modeling is the name given to the 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.

Example Copyright © 2012 Pearson Education, Inc.

Solving for Linear Model Parameters Copyright © 2012 Pearson Education, Inc.

Problem Solving Applied: Ozone Measurements* Copyright © 2012 Pearson Education, Inc.

Problem Solving Applied: Ozone Measurements* Copyright © 2012 Pearson Education, Inc.

Problem Solving Applied: Ozone Measurements* Copyright © 2012 Pearson Education, Inc.

Problem Solving Applied: Ozone Measurements* Copyright © 2012 Pearson Education, Inc.

Problem Solving Applied: Ozone Measurements* Copyright © 2012 Pearson Education, Inc.