Piyush Kumar. input data. both cout and cin are data objects and are defined as classes ( type istream ) class

Similar documents
Chapter 12 - C++ Stream Input/Output

Chapter 21 - C++ Stream Input/Output

I/O Streams and Standard I/O Devices (cont d.)

Chapter 21 - C++ Stream Input/Output

CS2141 Software Development using C/C++ Stream I/O

C++ Input/Output: Streams

Chapter 3 - Notes Input/Output

Streams. Rupesh Nasre.

Introduction to C++ (Extensions to C)

by Pearson Education, Inc. All Rights Reserved. 2

The C++ Language. Output. Input and Output. Another type supplied by C++ Very complex, made up of several simple types.

Module C++ I/O System Basics

Week 3: File I/O and Formatting 3.7 Formatting Output

Streams. Parsing Input Data. Associating a File Stream with a File. Conceptual Model of a Stream. Parsing. Parsing

Streams - Object input and output in C++

Streams in C++ Stream concept. Reference information. Stream type declarations

Coding Standards. Coding Standards. Coding Standards. Code Review. Optimization: Making it fast. Correctness

Software Design & Programming I

Input and Output. Data Processing Course, I. Hrivnacova, IPN Orsay

CS201 Solved MCQs.

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

We will exclusively use streams for input and output of data. Intro Programming in C++

Class 14. Input File Streams. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Introduction to Standard C++ Console I/O. C++ Object Oriented Programming Pei-yih Ting NTOU CS

Module 11 The C++ I/O System

Formatting outputs String data type Interactive inputs File manipulators. Access to a library that defines 3. instead, a library provides input

10/23/02 21:20:33 IO_Examples

Chapter 12. Streams and File I/O. Copyright 2016 Pearson, Inc. All rights reserved.

More File Operations. Lecture 17 COP 3014 Spring april 18, 2018

basic_fstream<chart, traits> / \ basic_ifstream<chart, traits> basic_ofstream<chart, traits>

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

CS242 COMPUTER PROGRAMMING

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

String Variables and Output/Input. Adding Strings and Literals to Your Programming Skills and output/input formatting

C++ does not, as a part of the language, define how data are sent out and read into the program

C++ Input/Output Chapter 4 Topics

Chapter Overview. I/O Streams as an Introduction to Objects and Classes. I/O Streams. Streams and Basic File I/O. Objects

Objects and streams and files CS427: Elements of Software Engineering

UEE1303(1070) S 12 Object-Oriented Programming in C++

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 12. Streams and File I/O. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Fig: iostream class hierarchy

PIC10B/1 Winter 2014 Exam I Study Guide

3.1. Chapter 3: Displaying a Prompt. Expressions and Interactivity

Chapter 6. I/O Streams as an Introduction to Objects and Classes. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Java IO and C++ Streams

Physics 6720 I/O Methods October 30, C++ and Unix I/O Streams

Definition Matching (10 Points)

Getting started with C++ (Part 2)

Today in CS162. External Files. What is an external file? How do we save data in a file? CS162 External Data Files 1

CS3157: Advanced Programming. Outline

The C++ Input/Output Class Hierarchy

Input/Output Streams: Customizing

Fall 2017 CISC/CMPE320 9/27/2017

C++ As A "Better C" Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

File I/O Christian Schumacher, Info1 D-MAVT 2013

File I/O. File Names and Types. I/O Streams. Stream Extraction and Insertion. A file name should reflect its contents

Chapter 3: Input/Output

Object Oriented Programming In C++

CS103L SPRING 2018 UNIT 7: FILE I/O

Writing a Good Program. 7. Stream I/O

Expressions, Input, Output and Data Type Conversions

We can even use the operator << to chain the output request as:

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

Fundamentals of Programming Session 25

1 Introduction to C++ C++ basics, simple IO, and error handling

Chapter 12. Streams and File I/O

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Lecture 5 Files and Streams

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

Lecture 9. Introduction

Standard I/O in C and C++

COMP322 - Introduction to C++

Advanced I/O Concepts

Setting Justification

cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... C++ vs Java identifiers.

Fundamentals of Programming Session 27

UNIT- 3 Introduction to C++

IS 0020 Program Design and Software Tools

Unit 1 : Principles of object oriented programming

Chapter 15 - C++ As A "Better C"

Object Oriented Programming Using C++ UNIT-3 I/O Streams

C++ Programming Lecture 10 File Processing

2 nd Week Lecture Notes

Strings and Stream I/O

What we will learn about this week:

Introduction. Lecture 5 Files and Streams FILE * FILE *

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

COMP322 - Introduction to C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Unit-V File operations

BITG 1233: Introduction to C++

The C++ Language. Arizona State University 1

Chapter 11 Customizing I/O

CHAPTER 3 Expressions, Functions, Output

CPE Summer 2015 Exam I (150 pts) June 18, 2015

CS11 Advanced C++ Lecture 2 Fall

Transcription:

C++ IO C++ IO All I/O is in essence, done one character at a time For : COP 3330. Object oriented Programming (Using C++) http://www.compgeom.com/~piyush/teach/3330 Concept: I/O operations act on streams (sequences) of ASCII characters Piyush Kumar C++ IO Interactive I/O cout standard output stream sequence of characters printed to the monitor input data output data cin standard input stream sequence of characters input from the keyboard Keyboard executing program Screen both cout and cin are data objects and are defined as classes cin ( type istream ) class cout ( type ostream ) class Example Example Namespaces: They provide a way to avoid name collision. Be careful about using this. Ostream object named cout. int main(void){ cout << Hello World ; cout << endl; Standard IO library for C++. Defines two fundamental types, istream and ostream. Stream: A flow of characters (1 or 2 bytes long). Can flow in and out of Files, strings, etc. int main(void){ cout << Hello World ; cout << endl; Equivalent to: operator<< (cout, Hello World ); Its calling a friend function of ostream with input data. Uses function declaration (approx): ostream& operator<<( ostream&, const char * ) 1

Example int main(void){ cout << endl; invokes a manipulator function called endl. endl looks something like this: ostream& endl( ostream& os) { os << '\n'; os.flush(); return os; Equivalent Compiler statement: std::cout.operator<<( std::endl(std::cout) ); Scope Operator for namespaces. Special Output Characters \n new line \t tab \b backspace \r carriage return \ʹ single quote \ʺ double quote \\ backslash Stream IO headers A Stream iostream -- contains basic information required for all stream I/O operations iomanip used for performing formatted I/O with stream manipulators fstream used for performing file I/O operations stringtream -- used for performing inmemory I/O operations (i.e., into or from strings in memory) A flow of characters. Buffers: IO to streams goes thru a buffer. C++ allows you change the default behavior of associated buffers. State: Each stream is associated with a state indicating various things like if an error has occurred or not C++ IO Class hierarchy C++ IO Class hierarchy ios ios istream ostream streambuf istream cin ostream cout streambuf ifstream istringstream ostringstream ofstream ifstream istringstream ostringstream ofstream stringbuf stringbuf filebuf filebuf 2

C++ IO Hierarchy The ios hierarchy defines the interface of the IO system. The streambuf hierarchy defines the implementation of the IO system, mostly provides the facilities of buffering and byte-level I/O Other Predefined Streams cerr - the standard destination for error messages (often the terminal window). Output through this stream is unit-buffered, which means that characters are flushed after each block of characters is sent. clog - like cerr, but its output is buffered. Formatting with predefined streams. Remember: Due to inheritance, anything you learn about formatting IO with predefined streams (cin, cout, clog, cerr) also applies to file IO and string IO. Anything available or defined in the ios class is available everywhere in the IO subsystem. Stream IO Inside ios << (left-shift operator) Overloaded as stream insertion operator >> (right-shift operator) Overloaded as stream extraction operator Both operators used with cin, cout, cerr, clog, and with user-defined stream objects Example cin >> Variable; cout << Variable; clog << Variable; Buffered cerr << Variable; Unbuffered, Variable immediately. Note: Variable types are available to the compiler. << operator << is overloaded to work on built-in types of C++. Can also be used to output userdefined types. Other interesting examples: cout << \n ; // newline. cout << 1+2= << (1+2) << endl; cout << endl; // newline. cout << flush; // flush the buffer. 3

<< operator Stream insertion: One char. Associates from left to right, and returns a reference to its left-operand object (i.e. cout). This enables cascading. Outputs char * type as a string. If you want to print the address, typecast it to (void *). Example: char name[] = cop3330 ; cout << name << static_cast<void *>( name ) << endl; static_cast<void *>( name ) equivalent to ((void *) name) in C except that it happens at compile time. put member function Outputs one character to specified stream cout.put( C'); Returns a reference to the object that called it, so may be cascaded cout.put( C' ).put( '\n' ); May be called with an ASCII-valued expression cout.put( 65 ); Outputs A Input Stream Input Stream : Looping >> (stream-extraction) Used to perform stream input Normally ignores whitespaces (spaces, tabs, newlines) Returns zero (false) when EOF is encountered, otherwise returns reference to the object from which it was invoked (i.e. cin) This enables cascaded input cin >> x >> y; >> controls the state bits of the stream failbit set if wrong type of data input badbit set if the operation fails while (cin >> fname) >> returns 0 (false) when EOF encountered and loop terminates. Example Program using std::cout; using std::cin; using std::endl; int main(void) { int height = 0, maxheight = 0; cout << "Enter the heights: (enter end of file to end): "; while(cin >> height) if( height > maxheight) maxheight = height; Output $./a.exe Enter the heights: (enter end of file to end): 72 89 54 33 68 66 Tallest person's height = 89 cout << "Tallest person's height = " << maxheight << endl; 4

istream member function: get istream member function: get get (array_name, max_size) ; char ch = cin.get(); Inputs a character from stream (even white spaces) and returns it. cin.get( c ); Inputs a character from stream and stores it in c char fname[256] cin.get (fname, 256); Read in up to 255 characters and inserts a null at the end of the string fname". If a delimiter is found, the read terminates. The array acts like a buffer. The delimiter is not stored in the array, but is left in the stream. istream member function: getline (array_name, max_size) char fname[256] cin.getline (fname, 256); Same as get, except that getline discards the delimiter from the stream. istream member functions: ignore() cin.ignore ( ) ; Discards one character from the input stream. cin.ignore (10) ; Discards 10 characters. cin.ignore(256, \n ); Discards 256 characters or newline, whichever comes first. istream member functions: peek(), putback() char ch = cin.peek ( ) ; Peeks into the stream s next character. cin.putback ( A ) ; Puts A back in the stream. FILE IO Example. Copy File first.txt into second.txt. #include <fstream> int main(void) { ifstream source("first.txt"); ofstream destin("second.txt"); char ch; while (source.get(ch)) destin<<ch; 5

Slightly modified #include <fstream> int main(void) { ifstream source("first.txt"); ofstream destin("second.txt"); char ch; while (source.peek()!= EOF){ source.get(ch); destin.put(ch); More IO functions read() cin.read(fname, 255); Reads 255 characters from the input stream. Does not append \0. cout.write(fname,255); Writes 255 characters. gcount: returns the total number of characters read in the last input operation. C++ IO Class Hierarchy Revisited ios Another example #include <sstream> #include <string> What if I replace this with istringstream? istream cin ostream cout ifstream istringstream iostream stringstream ostringstream fstream ofstream int main() { int i; string line; while(getline(cin,line)){ stringstream sfstream(line); while (sfstream >> i){ cout << i << endl; stringstream operations stringstream strm; stringstream strm(mystring); Initializes strm with a copy of mystring. strm.str(); Returns the content of strm in string format. Stream Manipulators #include <iomanip> strm.str(s); Copies the string s into strm. Returns void. 6

dec, hex, oct, setbase Formatting Output - Integers oct, hex, dec Cout << hex << 15; Prints F cout << setbase(16) << 15; Prints F again. int numstdts = 35533; cout << FSU has" << numstdts << "students." FSU has35533students. default field width == minimum required default: what happens when explicit formatting is not specified Formatting Output - Integers p.2 we can specify the field width, or number of spaces used to print a value cout << FSU has" << setw(6) << numstdts << " students." FSU has 35533 students. function call in field width 6, right-justified Formatting Output - Integers p.3 cout << FSU has" << setw(10) << numstdts << " students." FSU has 35533 students. in field width 10, right-justified Formatting Output - Integers p.4 cout << left; // flip to left justification cout << FSU has " << setw(10) << numstdts << "students." FSU has 35533 students. in field width 10, left-justified Using the default - Integers p.5 Note on field widths: if a field width specified is too small, or is not specified, it is automatically expanded to minimum required numstdts = 100; cout << FSU has " << numstdts << " students." FSU has 100 students. and works for any value of numstdts 7

General Rule of Thumb When you are printing numeric values in sentences or after a verbal label, the default field width usually works well When you are printing numeric values lined up in columns in a table, it is usually necessary to call setw to generate wellformatted output (we will see examples of this later in the course) Formatting Output - Reals float cost = 5.50; cout << "Cost is $" << cost << "today." Cost is $5.5today. default large values printed in scientific notation if number is whole, no decimal point numbers of digits not under your control Formatting Output - Reals p.2 Formatting Output - Reals p.3 Setting up real formatting // use fixed point notation cout << fixed; // print a decimal point (with whole numbers) cout << showpoint; ( noshowpoint ) these remain in effect until changed explicity, as does setprecision. setw only changes next value printed. float cost = 5.50; cout << "Cost is $" << setw(5) << setprecision(2) << cost << " today." Cost is $ 5.50 today. if no field width is specified, minimum is used, just as for integers You can just do this, once: cout << fixed << showpoint << setprecision(2); and these settings will remain in effect throughout your program run Formatting Output - char default field width == 1 note: setw does have effect on char type data too. char ch = 'Q'; cout << '*' << ch << setw(3) << '*'; *Q * 8

Formatting Output - Strings default field width == number of characters in the string can use setw cout << setw(10) << "Hello"; Hello Useful Output Spacer const string BLANK = " "; cout << setw(10) << BLANK; 10 blanks consider this: const char BLANK = ' '; cout << setw(10) << BLANK; 10 blanks! Unitbuf manipulator If you want to flush every output cout << unitbuf << first << second << nounitbuf; #include <iomanip> Example Quiz A. 0.1, 1, 1.23457e+009 B. 0.100000, 1.000000, 1234567936.000000 C. 1.000000e-001, 1.000000e+000, 1.234568e+009 D. 0.100, 1.000, 1234567936.000 E. 0.10000000000000000555 int main() { const double tenth = 0.1; const float one = 1.0; const float big = 1234567890.0; cout << "A. " << tenth << ", " << one << ", " << big << endl; cout << "B. " << fixed << tenth << ", " << one << ", " << big << endl; cout << "C. " << scientific << tenth << ", " << one << ", " << big << endl; cout << "D. " << fixed << setprecision(3) << tenth << ", " << one << ", " << big << endl; cout << "E. " << setprecision(20) << tenth << endl; Manipulators: Rolling your own. How to create our own stream manipulators? bell ret (carriage return) tab endline An Example. #include <ostream> Copy not allowed on ostreams. ostream& myendl( ostream& os) { os << "test\n"; os.flush(); return os; Stream Error States int main(void) { cout << myendl; 9

Error states Error States Example strm::eofbit if (cin.eof() == true) break; // stream end of file. strm::failbit if ( cin.fail() == true) break; // stream format error. strm::badbit If (cin.bad() == true) break; // data lost! Goodbit? cin.good() = ((!eofbit) && (!failbit) && (!badbit)) All eofbit, failbit and badbit should be false. cin.clear() // makes cin good. int ival; while ( cin >> ival,!cin.eof() ){ Assert(!cin.bad(), IO stream corrupted ); if (cin.fail()){ //bad input cerr << Bad data, try again. ; cin.clear(istream::failbit); // reset the stream continue; // ok to process ival now //end of while. Operators for testing. Interactive Input operator! Returns true if badbit or failbit set Useful for file processing if (! readmefile ) cerr << Error ; Write a prompt make it friendly and informative prompt typically contains prefix character to signal point at which to enter input Read value(s) user types data at keyboard Interactive Input: Example Interactive Input: Contents of Output Window int num; char response; prefix Enter a number -> 17<return> cout << "Enter a number -> "; cin >> num; cout << "Enter Y or N -> "; cin >> response; Enter Y or N -> Y<return> the program will not process the input until the return key is struck 10

Arguments: Count, Vector int main(int argc, char** argv) { std::cout << Argument Count: " << argc << std::endl; #include <stdlib.h> Another C++ Program (Hello argv[1]) // Print Argument Vector for (int i = 0; i < argc; ++i) { std::cout << argv[i] << std::endl; int main(int argc, char *argv[]) { if (argc!= 2) { cout << "Usage: hi.exe <name>" << endl; exit (1); cout << "Hello " << argv[1] << endl; Control structures Statements you should already know : While For If Recommended Assignments: 1.17, 1.25 11