1 Today’s Objectives  Announcements Turn in Homework 4 Quiz 4 will be on Wednesday, July 19 – It will have questions about inheritance, polymorphism,

Slides:



Advertisements
Similar presentations
CPS120: Introduction to Computer Science INPUT/OUTPUT.
Advertisements

File streams Chapter , ,
I/O and Program Control Statements Dick Steflik. Overloading C++ provides two types of overloading –function overloading the ability to use the same function.
 2006 Pearson Education, Inc. All rights reserved Stream Input/Output.
CS-212 C++ I/O Dick Steflik. C++ I/O Modeled after UNIX’s concept of a “stream” –conceptionally a stream is a continuous flow of characters/bytes from.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
計算機概論實習 Stream Stream: sequence of bytes Input: from device (keyboard, disk drive) to memory Output: from memory to device (monitor, printer,
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 Chapter 21 - C++ Stream Input/Output Outline 21.1Introduction.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 3: Input/Output.
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.
Chapter 3: Input/Output
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 1Winter Quarter I/O Manipulation Lecture.
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.
Stream Handling Streams - means flow of data to and from program variables. - We declare the variables in our C++ for holding data temporarily in the memory.
1 Advanced Input and Output COSC1567 C++ Programming Lecture 9.
CHAPTER 3 INPUT/OUTPUT. In this chapter, you will:  Learn what a stream is and examine input and output streams  Explore how to read data from the standard.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 12 - C++ Stream Input/Output Outline 12.1 Introduction 12.2 Streams Classic Streams vs.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
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.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 11- C++ Stream Input/Output Outline 11.1Introduction 11.2Streams Iostream Library Header.
Lecture Contents I/O Streams. –Input/output streams. –Unformatted vs formatted streams. –Stream manipulators. –Stream error state. –Stream tying. –Examples.
4. Input/Output Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 C++ Input/Output: Streams The.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 31 Thanks for Lecture Slides: C How to Program by Paul Deital &
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Basics Outline 21.1Introduction 21.2Streams Iostream Library.
Advanced Input and Output Object-Oriented Programming Using C++ Second Edition 10.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester 1436 King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah Alakeel.
Chapter 3: Input/Output
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 3 – Inventory Application: Introducing Variables,
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
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.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
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.
1 COMS 261 Computer Science I Title: Functions Date: October 24, 2005 Lecture Number: 22.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
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.
Streams and File Processing in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
C++ How to Program, 8/e © Copyright by Pearson Education, Inc. All Rights Reserved.
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
Chapter 13 Stream Input/Output C++ How to Program, 9/e © Copyright 2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor Note:
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.
CS212: Object Oriented Analysis and Design
Topic 2 Input/Output.
Introduction to C++ (Extensions to C)
EGR 2261 Unit 3 Input/Output Read Malik, Chapter 3.
Basic Input and Output Operations
Chapter 21 - C++ Stream Input/Output
Chapter 2 part #3 C++ Input / Output
Standard Input/Output Streams
Standard Input/Output Streams
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
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.
Chapter 5 Input and Output Streams
Chapter 3: Input/Output
Chapter 12 - C++ Stream Input/Output
Chapter 3 Input output.
Chapter 2 part #3 C++ Input / Output
Today’s Objectives 28-Jun-2006 Announcements
Chapter 1 c++ structure C++ Input / Output
Input/Output Streams, Part 1
Introduction to Programming
Presentation transcript:

1 Today’s Objectives  Announcements Turn in Homework 4 Quiz 4 will be on Wednesday, July 19 – It will have questions about inheritance, polymorphism, templates, and stream I/O Homework 5 is due on next Monday, July 24. Since this date is so close to the end of the semester, no late assignments will be accepted and will NOT be accepted!  Stream Input/Output (Ch. 15) Streams I/O in C++ with streams Stream error states Stream manipulators  Bonus Lab 8 – Debugging with MS Visual Studio 17-Jul-2006

2 Stream Input/Output Chapter 15

3 Streams  Stream = a sequence of bytes Just like a water-filled stream, a C++ stream has a flow, it can be directed, and it can be stopped. But it cannot flow backwards.  In C++, I/O is accomplished by using streams A C++ stream is an object – it’s instantiated from a class, just like any other object Output – the bytes flow from memory to an output device Input – the bytes flow from an input device to memory Stream I/O (Deitel, 771; Josuttis, 584)

4 I/O in C++  Low-level I/O is unformatted I/O Specifies the number of bytes to transfer Example: member functions such as “get”, “put”, “read”, and “write”  High-level I/O is formatted I/O Uses bytes in groups corresponding to built-in data types Example: int n; cin >> n;  wchar_t = a C++ data type used to store Unicode characters Stream I/O (Deitel, 771–772)

5 Header Files Basic stream I/O services Defines objects: cout, cin, cerr, and clog Services for formatted I/O with stream manipulators Services for file I/O Stream I/O (Deitel, 772)

6 Stream I/O Classes  Different classes are used to create stream objects for each type of I/O, Fig. 15.1, page 773  istream Defines input streams Instantiation of the template class basic_istream<> using char as the type parameter  ostream Defines output streams Instantiation of the template class basic_ostream<> using char as the type parameter Stream I/O (Deitel, 772–773; Josuttis, 585)

7 Global Stream Objects cin Predefined object of the istream class Used for standard user input, normally connected to the keyboard cout Predefined object of the ostream class Used for standard user output, normally connected to the monitor cerr Predefined object of the ostream class Used for error messages, not buffered, normally connected to the monitor clog Predefined object of the ostream class Used for logging messages, buffered, normally connected to the same device as cerr Stream I/O (Deitel, 772–773; Josuttis, 585)

8 Stream Output  operator<< Stream insertion operator Overloaded for output to ostream objects It’s typesafe because it’s overloaded for all the built-in data types, and it can be overloaded for our own classes  Member function put Member function of the ostream class Used to output single chars Examples cout.put( 'A' ); cout.put( 65 ); //Using the ASCII value Stream I/O (Deitel, 775–776; Josuttis, 586)

9 Stream Input  operator>> Stream extraction operator Overloaded for input from istream objects It’s typesafe because it’s overloaded for all the built-in data types, and it can be overloaded for our own classes Ignores whitespace and leaves it in the stream  Member functions of the istream class get, getline ignore eof, gcount putback, peek Stream I/O (Deitel, 776–780; Josuttis, 586)

10 Using get()  Used with no arguments, it inputs one char char c = cin.get();  Can be used with three arguments 1. char array 2. size limit 3. delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.get(buffer,1024,'\n'); Stream I/O (Deitel, 776–780)

11 Using get()  Used with no arguments, it inputs one char char c = cin.get();  Can be used with three arguments 1. char array 2. size limit 3. delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.get(buffer,1024,'\n'); buffer \n H e ll o cin Stream I/O (Deitel, 776–780)

12 Using get()  Used with no arguments, it inputs one char char c = cin.get();  Can be used with three arguments 1. char array 2. size limit 3. delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.get(buffer,1024,'\n'); buffer H e l lo \n cin Stream I/O (Deitel, 776–780)

13 Using get()  Used with no arguments, it inputs one char char c = cin.get();  Can be used with three arguments 1. char array 2. size limit 3. delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.get(buffer,1024,'\n'); buffer H l l o\n e cin Stream I/O (Deitel, 776–780)

14 Using get()  Used with no arguments, it inputs one char char c = cin.get();  Can be used with three arguments 1. char array 2. size limit 3. delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.get(buffer,1024,'\n'); buffer H l o \n el cin Stream I/O (Deitel, 776–780)

15 Using get()  Used with no arguments, it inputs one char char c = cin.get();  Can be used with three arguments 1. char array 2. size limit 3. delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.get(buffer,1024,'\n'); buffer H o \n ell cin Stream I/O (Deitel, 776–780)

16 Using get()  Used with no arguments, it inputs one char char c = cin.get();  Can be used with three arguments 1. char array 2. size limit 3. delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.get(buffer,1024,'\n'); buffer H \n ello Leaves the delimiter in the stream cin Stream I/O (Deitel, 776–780)

17 Using get()  Used with no arguments, it inputs one char char c = cin.get();  Can be used with three arguments 1. char array 2. size limit 3. delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.get(buffer,1024,'\n'); buffer cin H \n ello\0 Inserts ‘\0’ terminator Stream I/O (Deitel, 776–780)

18 Using getline()  Used with three arguments 1. char array 2. size limit 3. delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.getline(buffer,1024); Stream I/O (Deitel, 776–780)

19 Using getline()  Used with three arguments 1. char array 2. size limit 3. delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.getline(buffer,1024); buffer \n H e ll o cin Stream I/O (Deitel, 776–780)

20 Using getline()  Used with three arguments 1. char array 2. size limit 3. delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.getline(buffer,1024); buffer H e l lo \n cin Stream I/O (Deitel, 776–780)

21 Using getline()  Used with three arguments 1. char array 2. size limit 3. delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.getline(buffer,1024); buffer H l l o\n e cin Stream I/O (Deitel, 776–780)

22 Using getline()  Used with three arguments 1. char array 2. size limit 3. delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.getline(buffer,1024); buffer H l o \n el cin Stream I/O (Deitel, 776–780)

23 Using getline()  Used with three arguments 1. char array 2. size limit 3. delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.getline(buffer,1024); buffer H o \n ell cin Stream I/O (Deitel, 776–780)

24 Using getline()  Used with three arguments 1. char array 2. size limit 3. delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.getline(buffer,1024); buffer H \n ello Discards the delimiter in the stream cin Stream I/O (Deitel, 776–780)

25 Using getline()  Used with three arguments 1. char array 2. size limit 3. delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.getline(buffer,1024); buffer Hello\0 Inserts ‘\0’ terminator cin Stream I/O (Deitel, 776–780)

26 Using read() and write()  Input Member function read of the istream class Inputs bytes into a char array  Output Member function write of the ostream class Outputs bytes in a char array  Example char buffer[1024]; cin.read( buffer, 5 ); cout.write( buffer, 5 ); Stream I/O (Deitel, 776–780)

27 Stream Error States  A data member of the stream objects identifies whether I/O was successful with the following values  goodbit = everything okay, normally set to 0  eofbit = end-of-file encountered  failbit = operation not successful, but the stream is okay, e.g. char in the stream instead of an integer  badbit = operation not successful, because stream is corrupted or lost, e.g. reading past the end of a file Stream I/O (Deitel, 766–768; Josuttis 597)

28 Determining the Stream State  Use the following member functions to determine the state of the flags  good() = returns true if the stream is okay  eof() = returns true if the end-of-file was encountered  fail() = returns true if there was an I/O error  operator! = same as fail()  bad() = returns true if there was a fatal error  clear() = clears all flags Stream I/O (Deitel, 797–799; Josuttis 597)

29 Example: Checking Stream State int main(){ int n = 1; char c = 'x'; while( n > 0 ){ cout << "Enter an integer: "; cin >> n; if( !cin ){ //check the failbit cout << "That was not an integer!\n"; cin.clear(); //clear the flags cin >> c; //something is still in the stream cout << "You entered " << c << '\n'; } else cout << "You entered " << n << '\n'; } Stream I/O

30 Stream Manipulators  Special objects used to format a stream  Used with the stream insertion operator or stream extraction operator Stream I/O (Deitel, 781–796; Josuttis, 586) cout << setw(10) << right << "Hello" << endl; Manipulators that do not require arguments will not need an extra header file. Manipulators that require arguments will need the header file.

31 Some Stream Manipulators ManipulatorMeaning setw()Set the width of the output leftLeft-justify the output rightRight-justify the output setprecision()Set the number of digits to the right of the decimal point in floating point numbers showpointSpecifying that floating point numbers are always output with a decimal point fixedUse fixed-point notation for the output format of floating point numbers scientificUse scientific notation for the output of floating point numbers uppercaseSet the ‘E’ in scientific notation to uppercase Stream I/O (Deitel, 781–796; Josuttis, 586)

32 Member Functions for Formatting  Streams have member functions that can be used to set their format Can be used in combination with stream manipulators Some member functions do the same thing as the manipulators Examples: width(n) Sets the width of the output to n Equivalent to the “ setw(n) ” stream manipulator cout.width(10); precision(n) Sets the width of the output to n Equivalent to the “ setprecision(n) ” stream manipulator cout.precision(2); Stream I/O (Deitel, 781–796; Josuttis, 586)

33 Bonus Lab 8 Debugging with MS Visual Studio Ford and Teorey, Ch. 5

34 Debugging Methods  Semantic errors Logic errors in your code Examples: code that creates an infinite loop, off-by- one error  Interactive debugger Can run your program line by line You can stop program execution at any point and examine the values of its variables Debugging (Ford)

35 Using the MS Visual Studio Interactive Debugger  Use “Debug” mode  Step Into Executes the next statement Will start at the first line in main if there’s no breakpoint If the next statement is a function call, it will enter the function  The yellow arrow Points to the next statement that will be executed  Step Over Executes the next statement If the next statement is a function call, it will skip over the function  Step Out Will finish execution of a function and run to the next statement  Breakpoint The line where you want your program to stop execution Set by clicking in the margin at the left of the line of code  Variables pane Displays a list of current variables and their values Auto, Locals, and Watch tabs Debugging (Ford)

36 References Deitel, H. M., and P. J. Deitel, C++ How to Program, Fifth Edition. Upper Saddle River, NJ: Prentice Hall, Ford, A. R., and T. J. Teorey, Practical Debugging in C++. Upper Saddle River, NJ: Prentice Hall, Josuttis, Nicolai M., The C++ Standard Library, A Tutorial and Reference. Boston: Addison-Wesley, 1999.