Managing console i/o operations

Slides:



Advertisements
Similar presentations
Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
Advertisements

CPS120: Introduction to Computer Science INPUT/OUTPUT.
CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.
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.
1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
16/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files All the data used in our programs so far has been entered through the keyboard and.
Simplest program and Data Output Sen Zhang. The simplest program looks like a single person company! void main() { // this starts a comment line // here.
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
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.
Unformatted and Formatted I/O Operations. 2 Unformatted Input/output is the most basic form of input/output. Unformatted I/O transfers the internal binary.
UNFORMATTED INPUT OUTPUT. Topics to be discussed……………….. overloaded operators >> and and
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.
FORMATTED INPUT OUTPUT. Topics to be discussed……………….. Formatted Console I/O operationsFormatted Console I/O operations Defining field width :Width()Defining.
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.
Chapter 14 Stream Input and Output By C. Shing ITEC Dept Radford University.
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.
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:
Chapter 9 Streams: Input stream: source of characters. Output stream: destination for characters. Up to now the input stream has defaulted to the keyboard.
Lecture Contents I/O Streams. –Input/output streams. –Unformatted vs formatted streams. –Stream manipulators. –Stream error state. –Stream tying. –Examples.
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 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.
#include int main() { char *string1=”C++”; char *string2=”Program”; int m=strlen(string1); int n=strlen(string2); for(int i=1;i
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.
Computer Programming II Lecture 9. Files Processing - We have been using the iostream standard library, which provides cin and cout methods for reading.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
Streams & Files in C++:Stream Classes, stream errors, disk file I/O with streams, File Pointers, Error handling in file I/O, File I/O with member functions,
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني I/O and File management Concept of streams. cin and cout objects. C++stream classes. Unformatted I/O.
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)
C++ Basic Input and Output (I/O)
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Chapter 3 L7.
TMF1414 Introduction to Programming
EGR 2261 Unit 3 Input/Output Read Malik, Chapter 3.
Chapter 3: Expressions and Interactivity.
Output Stream Formatting
Chapter 21 - C++ Stream Input/Output
Chapter 2 part #3 C++ Input / Output
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.
files Dr. Bhargavi Goswami Department of Computer Science
Chapter 3: Input/Output
Introduction to cout / cin
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
Chapter 3: Expressions and Interactivity
Chapter 4 INPUT AND OUTPUT OBJECTS
Programming with ANSI C ++
Chapter 2 part #3 C++ Input / Output
C++ Programming Lecture 8 File Processing
Introduction to cout / cin
Input/Output Streams, Part 2
Presentation transcript:

Managing console i/o operations Dr. Bhargavi Goswami Department of Computer Science Christ University Bangalore. bhargavigoswami@gmail.com 9426669020

Introduction How to control the way output is printed? C++ supports rich set of I/O functions and operations. How? C++ uses the concept of stream and stream classes. Application? Terminals, Disks, Tape Drives, etc. This interface is called stream. What is steam? It is a sequence of bytes. It acts as a source (input stream) or destination (output stream). We already have cin and cout. Then why we need this? Cin and Cout is there, but only for screen, not for files. Again, it does not give control of entire screen.

C++ stream classes C++ I/O system has hierarchy of classes to deal with console and disk files. Header file? iosteam. Class ios is Virtual Base class. Why? So that only one copy is inherited by iostream. What are the functions of each class? Lets see next table.

Unformatted i/o operations >> is overloaded in istream class. << is overloaded in ostream class. Classes istream and ostream define two member functions get() and put(). We can read and display a line of text more efficiently using line oriented IO functions with getline() and write(). Sytx: cin.getline(line,size); Sytx:cout.write(line,size); Lets see the examples: 10-1-CharacterIOWithGet()AndPut().cpp 10-2-DisplayingStringWithWrite().cpp 10-3-ReadingStringWithGetline.cpp

formatted console i/o operation C++ supports options for formatting output. How? Using… 1) ios class functions and flags 2) Manipulators 3) User defined output functions See next table for functions and further table for manipulators before checking the example programs. In addition to these functions by c++ library, we can create our own user defined and pre-defined formatting functions. Width Function: We need obj to invoke it. W is width of field. Stx: cout.width(w); See the example Program. 10-4-SpecifingFieldSizeWithWidth.cpp.

Continue….. formatted console i/o operation In C++, by default the numbers are printed with six digits after decimal point. Can we control it? Yes. How? Using precision() member function. Stx: cout.precision(d); where d is number of digits. See the examples: 10-5-PrecisionSettingWithPrecision.cpp 10-6-WidthAndPrecisionManipulators.cpp

Continue….. formatted console i/o operation In C++, unused positions of the field are filled with white spaces by default. Again, to control the filling of white space can be done. How? By using function fill(). Stx: cout.fill(ch); See the example program: 10-7-PaddingWithFill.cpp

Continue….. formatted console i/o operation In C++, if we want to print the text left-justified, what is the option? How do we get a floating point number printed in scientific notation? Ans: set flags using setf() functions. Stx: cout.setf(arg1,arg2); Check the table having Flags with Bit fields. Check the table having Flags without Bit fields. See the following programs: 10-8-ManipulationOfFlagAndBitFields.cpp 10-9-FormatingWithFlagsInSetf.cpp

Flags and bit fields for setf() function

Flags that do not have bit fields

Managing output with manipulators Does alternate of ios member functions and flags exist? Yes. What is that? Manipulators provided in header file “iomanip”. Then what is the difference between manipulators and ios functions? ios functions return to previous format state. But, manipulators does not return to previous format state. Explicit restore operation has to be performed for manipulators. Check the table having Manipulators. Prog. 10 has manipulators and ios functions also. We can also design our own manipulators. See Prog 11 See the following programs: 10-10-FormatingWithManipulators.cpp 10-11-User-DefinedManipulators.cpp

Manipulators with meaning

Thank you Next Chapter will start from tomorrow. Start your preparations now.