Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 1Winter Quarter I/O Manipulation Lecture.

Slides:



Advertisements
Similar presentations
CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.
Advertisements

File streams Chapter , ,
 2006 Pearson Education, Inc. All rights reserved Stream Input/Output.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
CS 1400 Feb 2007 Pick ups from chapters 2 and 3. Example; A person 15 years of age may acquire a driver’s lic. with permission from parents. A person.
計算機概論實習 Stream Stream: sequence of bytes Input: from device (keyboard, disk drive) to memory Output: from memory to device (monitor, printer,
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
© 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
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Outline 21.1Introduction 21.2Streams Iostream Library Header.
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.
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.
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.
File I/O ifstreams and ofstreams Sections 11.1 &
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 1Winter Quarter C++: I/O and Classes Lecture 25.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
Input/Output Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics 2 I/O - Input/Output is one of the first aspects of programming that needs to be mastered:
 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.
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.
1 Today’s Objectives  Announcements Turn in Homework 4 Quiz 4 will be on Wednesday, July 19 – It will have questions about inheritance, polymorphism,
Chapter 3: Input/Output
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.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
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.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
C++ How to Program, 8/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Topic 2 Input/Output.
Introduction to C++ (Extensions to C)
C++ Basic Input and Output (I/O)
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Chapter 21 - C++ Stream Input/Output Stream Manipulators
Output Stream Formatting
Basic Input and Output Operations
Chapter 21 - C++ Stream Input/Output
Chapter 2 part #3 C++ Input / Output
Input and Output Chapter 3.
Standard Input/Output Streams
Standard Input/Output Streams
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
Advanced Input and Output
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.
Programming with Data Files
Chapter 5 Input and Output Streams
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Chapter 3: Input/Output
Chapter 3 Input output.
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
Chapter 3: Expressions and Interactivity
Chapter 4 INPUT AND OUTPUT OBJECTS
Formatted Input, Output & File Input, Output
C++ Stream Input/Output
Chapter 2 part #3 C++ Input / Output
Input/Output Streams, Part 1
Input/Output Streams, Part 2
Presentation transcript:

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 1Winter Quarter I/O Manipulation Lecture 27

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 2Winter Quarter Stream I/O Library Header Files Note:There is no “.h” on standard header files. Be careful about “using namespace std” iostream -- contains basic information required for all stream I/O operations iomanip -- contains information useful for performing formatted I/O with parameterized stream manipulators fstream -- contains information for performing file I/O operations strstream -- contains information for performing in- memory I/O operations (i.e., into or from strings in memory)

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 3Winter Quarter Classes for Stream I/O in C++ ios is the base class. istream and ostream inherit from ios ifstream inherits from istream (and ios) ofstream inherits from ostream (and ios) iostream inherits from istream and ostream (& ios) fstream inherits from ifstream, iostream, and ofstream

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 4Winter Quarter C++ Stream I/O -- Stream Manipulators C++ provides various stream manipulators that perform formatting tasks. Stream manipulators are defined in These manipulators provide capabilities for –setting field widths, –setting precision, –setting and unsetting format flags, –flushing streams, –inserting a "newline" and flushing output stream, –skipping whitespace in input stream

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 5Winter Quarter C++ Stream I/O -- Stream Manipulators setprecision ( ) Select output precision, i.e., number of significant digits to be printed. Example: cout << setprecision (2) ; // two significant digits setw ( ) Specify the field width (Can be used on input or output, but only applies to next insertion or extraction). Example: cout << setw (4) ;// field is four positions wide

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 6Winter Quarter C++ Stream I/O -- Stream Format States Various ios format flags specify the kinds of formatting to be performed during stream I/O. There are member functions (and/or stream manipulators) which control flag settings. There are various flags for trailing zeros and decimal points, justification, number base, floating point representation, and so on.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 7Winter Quarter Stream I/O Format State Flags ios::showpointwhen set, show trailing decimal point and zeros ios::showposwhen set, show the + sign before positive numbers ios::basefield ios::decuse base ten ios::octuse base eight ios::hexuse base sixteen

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 8Winter Quarter Stream I/O Format State Flags ios::floatfield ios::fixeduse fixed number of digits ios::scientificuse "scientific" notation ios::adjustfield ios::leftuse left justification ios::rightuse right justification ios::internalleft justify the sign, but right justify the value

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 9Winter Quarter Stream I/O Format State Flags ios::eofbitset when eof encountered [ stream.eof() ] ios::failbitset when format error occurredon the stream, but no characters were lost [ stream.fail() or simply ! stream ] ios::badbitset when stream error occurs that results in a loss of data [ stream.bad() ] ios::goodbitset when none of the bits eofbit, failbit, or badbit are set [ stream.good() ]

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 10Winter Quarter I/O Stream Member Functions.setf ( ) Allows the setting of an I/O stream format flag. Examples: // To show the + sign in front of positive numbers cout.setf (ios::showpos) ; // To output the number in hexadecimal cout.setf (ios::hex, ios::basefield) ;

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 11Winter Quarter I/O Stream Member Functions.precision ( ) ; Select output precision, i.e., number of significant digits to be printed. Example: cout.precision (2) ; // two significant digits.width ( ) ; Specify field width. (Can be used on input or output, but only applies to next insertion or extraction). Example: cout.width (4) ;// field is four positions wide

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 12Winter Quarter I/O Stream Member Functions.eof ( ) ; Tests for end-of-file condition. Example: cin.eof ( ) ; // true if eof encountered.fail ( ) ; Tests if a stream operation has failed. Example: cin.fail ( ) ;// true if a format error occurred ! cin;// same as above; true if format error

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 13Winter Quarter I/O Stream Member Functions.clear ( ) ; Normally used to restore a stream's state to "good" so that I/O may proceed or resume on that stream. Example: cin.clear ( ) ; // allow I/O to resume on a "bad" // stream, in this case "cin", // on which error had previously // occurred

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 14Winter Quarter Using Manipulators & Member Functions #include // No “.h” (standard header) using namespace std;// To avoid “std::” int main ( ) { int a, b, c = 8, d = 4 ; float k ; char name[30] ; cout << "Enter your name" << endl ; cin.getline (name, 30) ; cout << "Enter two integers and a float " << endl ; cin >> a >> b >> k ;

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 15Winter Quarter Using Manipulators & Member Functions // Now, let's output the values that were read in cout << "\nThank you, " << name << ", you entered" << endl << a << ", " << b << ", and " ; cout.width (4) ; cout.precision (2) ; cout << k << endl ; // Control the field and precision another way cout <<"\nThank you, " << name << ", you entered" << endl << a << ", " << b << ", and " << setw (c) << setprecision (d) << k << endl ; }

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 16Winter Quarter Example Program Output Enter your name R. J. Freuler Enter two integers and a float Thank you, R. J. Freuler, you entered 12, 24, and 68 Thank you, R. J. Freuler, you entered 12, 24, and 67.85

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 17Winter Quarter More Input Stream Member Functions.get ( ) ; Example: char ch ; ch = cin.get ( ) ;// gets one character from keyboard // & assigns it to the variable "ch".get (character) ; Example: char ch ; cin.get (ch) ;// gets one character from // keyboard & assigns to "ch"

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 18Winter Quarter More Input Stream Member Functions.get (array_name, max_size) ; Example: char name[40] ; cin.get (name, 40) ;// Gets up to 39 characters // and inserts a null at the end of the // string "name". If a delimiter is // found, the read terminates. The // delimiter is not stored in the array, // but it is left in the stream.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 19Winter Quarter More Input Stream Member Functions.getline (array_name, max_size) ; Example: char name[40] ; cin.getline (name, 40) ; // Gets up to 39 characters // and assigns the string to "name". A // null is inserted at the end of the string. // Note that if a delimiter is found, // it is removed from the stream, but it is // not stored in the character array.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 20Winter Quarter More Input Stream Member Functions.ignore ( ) ; Ex: cin.ignore ( ) ;// gets and discards 1 character cin.ignore (2) ;// gets and discards 2 characters cin.ignore (80, '\n') ;// gets and discards up to 80 // characters or until "newline" // character, whichever comes // first

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 21Winter Quarter More Input Stream Member Functions.peek( ) ; Ex: char ch ; ch = cin.peek ( ) ; // peek at (don't take) character.putback( ) ; Ex: char ch; cin.putback (ch) ; // put character back in stream

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 22Winter Quarter More I/O Stream Member Functions.read( ) ;.write( ) ; Ex: char gross[144] ; cin.read(gross,144) ; // reads 144 characters from // input stream. Does NOT // append '\0'

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 23Winter Quarter File I/O with C++ #include using namespace std; int main ( ) { int a, b, c ; ifstream fin ;//Create file input stream object fin.open ( "my_input.dat") ;//Open input file fin >> a >> b ;//Read two values from input file c = a + b ; ofstream fout ;//Create file output stream object fout.open ( "my_output.dat"); //Open output file fout << c << endl ;//Write result to output file fin.close ( ) ;//Close input file fout.close ( ) ;//Close output file }