Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Streams and Files Problem Solving, Abstraction, and Design using.

Slides:



Advertisements
Similar presentations
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1 Overview 8.1 An Array Type for Strings 8.2 The Standard string.
Advertisements

File Input/Output. External Files Batch –Requires use of data files (save to disk) –Batch can be run during off peak use –allows things to be complete.
File streams Chapter , ,
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
1 Lecture 19 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
Lecture 18:Topic: I/O Formatting and Files Dale/Weems/Headington
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 3- 1.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 3: Input/Output.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 3: Input/Output.
CS 117 Spring 2002 Using Files Hanly: Chapter 9, some in Chapter 4 Freidman-Koffman: Chapter 8, iomanip in Chapter 5.
Chapter 3: Input/Output
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
Streams, Files, and Formatting Chapter Standard Input/Output Streams t Stream is a sequence of characters t Working with cin and cout t Streams.
Chapter 8 Streams and Files Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University Millersville, PA
1 Chapter 4 Program Input and the Software Design Process.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
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.
Streams, Files, and Formatting Chapter Standard Input/Output Streams t Stream is a sequence of characters t Working with cin and cout t Streams.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
4. Input/Output Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 C++ Input/Output: Streams The.
No I/O is built into C++ l instead, a library provides input stream and output stream KeyboardScreen executing program istreamostream 1.
 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 3 Expressions and Interactivity.
1 Program Input Software Design Chapter 4. 2 You Will Want to Know... Prompting for and reading values into a program. Accessing data from a file. What.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
1 Program Input and the Software Design Process. 2 Chapter 4 Topics  Input Statements to Read Values for a Program using >>, and functions get, ignore,
Chapter 3: Input/Output
Chapter -7 Basic function of Input/output system basics and file processing Stream classes : I/O Streams. A stream is a source or destination for collection.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Formatting Output.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
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.
Streams One of the themes of this course is that everything can be reduced to simple (and similiar) concepts. Streams are one example. Keyboard and Screen.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
Program Input and the Software Design Process ROBERT REAVES.
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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Lecture 27: File Input/Output Processing. 2 Lecture Contents: t Input/Output files t Text files and Binary files t File access t Standard Input/Output.
1 Chapter 4 Program Input and the Software Design Process.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
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.
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.
Chapter Expressions and Interactivity 3. The cin Object 3.1.
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3: Expressions and Interactivity.
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.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
Chapter 3: Expressions and Interactivity.
Copyright © 2003 Pearson Education, Inc.
Input and Output Chapter 3.
Standard Input/Output Streams
Standard Input/Output Streams
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Today’s Lecture I/O Streams Tools for File I/O
Chapter 9 File Streams Computing Fundamentals with C++ 3rd Edition
Chapter 3: Input/Output
No I/O is built into C++ instead, a library provides input stream and output stream Keyboard Screen executing program istream ostream 1.
Chapter 3 Input output.
Chapter 4 Program Input and the Software Design Process
Formatted Input, Output & File Input, Output
CS150 Introduction to Computer Science 1
CHAPTER 4 File Processing.
Dale/Weems/Headington Program Input and the Software Design Process
Presentation transcript:

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Streams and Files Problem Solving, Abstraction, and Design using C++ 6e by Frank L. Friedman and Elliot B. Koffman

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Standard Input/Output Streams A stream is a sequence of characters Standard input stream (cin) and standard output stream (cout) of the iostream library Streams convert internal representations to character streams or vice versa >> input (extraction) operator << output (insertion) operator Any character (printable or not) A stream has no fixed size

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3 Input Stream, >> Operator Last character read is kept track using an input stream buffer pointer Each new input attempt begins at current pointer position Leading white space (blanks, tab, nwln) skipped until first non-white-space character is located cin >> ch; For numeric value, all characters that are part of the numeric value are processed until a character that’s not legally part of a C++ number is read.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 Reading One Character at a Time Can also read/write white space –iostream functions get and put

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 Listing 8.2 Processing individual characters in a stream

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6 Listing 8.2 Processing individual characters in a stream (continued)

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7 Table 8.1 The File Manipulation Member Functions

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8 Using a Stream in a Condition cout << “Enter a line or press “ << ENDFILE << “: “; while (cin.get(next))// get first char of new line { // insert loop body including inner while loop... cout << “Enter a line or press “ << ENDFILE << “: “; }// end outer while

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley External Files Interactive –Expect user input from keyboard –Ok for smaller programs Batch –Requires use of data files (save to disk) –Don’t expect input from keyboard, so no prompts –Often uses echo printing of input –Input file can be read many times –Can also write output to file –Output file from one program can be input to another

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10 Directory Names for External Files Must know location of file within directory structure (pathname) Directories are system dependent File names usually follow system and local conventions –how file is named –use of extensions.cpp.dat.doc.txt

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11 Attaching Streams to External Files Declaring a stream object ifstream ins; ofstream outs; External (physical) file name linked to internal (logical) name by open function #define inFile “InData.txt”... ins.open(inFile);

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 Reading a File Name string fileName; cout << “Enter the input file name: “; cin >> fileName; ins.open(fileName.c_str( ));

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13 Case Study: Preparing a Payroll File Write a program that reads a data file consisting of employee salary data, and computes the employee’s gross salary. It writes the employee’s name and gross salary to an output file and accumulates the gross salary amount in the total company payroll, which it also displays.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 14 Case Study: Sample Files Input Jim Baxter Adrian Cybriwsky Ayisha Mertens Output Jim Baxter Adrian Cybriwsky Ayisha Mertens160.00

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 15 Case Study: Problem Analysis Streams Used –ifstream eds// employee data info –ofstream pds// payroll data info Problem Input (from stream eds) –for each employee string firstName string lastName float hoursWorked float hourlyRate

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 16 Case Study: Problem Analysis Problem Output (to stream pds) –for each employee string firstName string lastName float salary Problem Output (to stream cout) –float totalPayroll// total company payroll

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 17 Case Study: Program Design Algorithm for function main 1. Prepare streams and associated files for processing 2. Process all employees and compute payroll total (function processEmp). 3. Display the payroll total.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18 Case Study: Interface for processEmp Input Arguments –ifstream eds// input stream - employee data –ofstream pds// output stream - payroll data Output Arguments –none Function Return Value –float totalPayroll// total company payroll

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 19 Case Study: Algorithm for processEmp 1. Initialize payroll total to While there are more employees 2.1 Read employee’s first and last names and salary data from eds 2.2 Compute employee’s salary 2.3 Write employee’s first and last names and salary to pds; add it to payroll total

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 20 Listing 8.5 Implementation of processEmp

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 21 Listing 8.5 Implementation of processEmp (continued)

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More on Reading String Data getline (istream& ins, string& str) getline (istream& ins, string& str, char delimiter) ins.ignore (int n, char delimiter)

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 23 Using getline getline - could be used to process an entire line of data E.g. for data file containing Jim Andrew Baxter# Adrian Cybriwsky# Ayisha W. Mertens# Use # as a delimiter character getline (eds, name, ‘#’);

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 24 Using ignore getline does not skip leading white space, so if a newline character is encountered at the beginning of the characters to be extracted, getline will stop immediately and won’t perform the expected task E.g. data file contains Jim Andrew Baxter Adrian Cybriwsky

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 25 payroll = 0.0; getline(eds, name); while (!eds.eof( )) { eds >> hours >> rate; salary = hours * rate; pds << name << “ “ << salary << endl; payroll += salary; eds.ignore(100, ‘\n’); getline(eds, name); }

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 26 Table 8.3 Input/Output Manipulators

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 27 Manipulators Example cout << “The value of x is “ << fixed << showpoint << setprecision(4) << x << endl;

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Common Programming Errors Connecting streams and external files Using input and output streams incorrectly Reading past the end of a file Matching data in an input stream to input data stores White space and input Proper handling of the newline character Input and output settings