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

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

Chapter 21 - C++ Stream Input/Output

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

Chapter 21 - C++ Stream Input/Output

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

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

Fig: iostream class hierarchy

What we will learn about this week:

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

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

C++ Programming Lecture 10 File Processing

Introduction to C++ (Extensions to C)

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

Streams and Basic File I/O Tools for Stream I/O Character I/O Inheritance

Chapter 3 - Notes Input/Output

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

Objects and streams and files CS427: Elements of Software Engineering

Chapter 12. Streams and File I/O

CS3157: Advanced Programming. Outline

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

Chapter 12 - C++ Stream Input/Output

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

Lecture 3. Input and Output. Review from last week. Variable - place to store data in memory. identified by a name should be meaningful Has a type-

Lab 6. Review of Variables, Formatting & Loops By: Dr. John Abraham, Professor, UTPA

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

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

C++ Input/Output: Streams

UNIT V FILE HANDLING

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

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

Engineering Problem Solving with C++, Etter/Ingber

Convenient way to deal large quantities of data. Store data permanently (until file is deleted).

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

10/23/02 21:20:33 IO_Examples

Writing a Good Program. 7. Stream I/O

Streams. Rupesh Nasre.

File Operations. Lecture 16 COP 3014 Spring April 18, 2018

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

File Input / Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #9

by Pearson Education, Inc. All Rights Reserved. 2

Streams - Object input and output in C++

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

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

C++ Input/Output Chapter 4 Topics

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 7. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

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

Module 11 The C++ I/O System

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

CS201 Solved MCQs.

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

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

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

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

COMP322 - Introduction to C++

COMP322 - Introduction to C++

CSCI 104 Overview. Mark Redekopp David Kempe

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.

PROGRAMMING EXAMPLE: Checking Account Balance

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

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

Object Oriented Programming In C++

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

Fall 2017 CISC/CMPE320 9/27/2017

Software Design & Programming I

Chapter 14 Sequential Access Files

Advanced I/O Concepts

Programming in C/C

Chapter 2. Outline. Simple C++ Programs

Random File Access. 1. Random File Access

IS 0020 Program Design and Software Tools

Chapter 3: Input/Output

Setting Justification

Lecture 9. Introduction

Input and Output File (Files and Stream )

Week 5: Files and Streams

Chapte t r r 9

Consider the following example where a base class has been derived by other two classes:

CSC 138 Structured Programming CHAPTER 4: TEXT FILE [PART 1]

Developed By : Ms. K. M. Sanghavi

EP241 Computing Programming

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

System Design and Programming II

Expressions, Input, Output and Data Type Conversions

CSc Introduc/on to Compu/ng. Lecture 19 Edgardo Molina Fall 2011 City College of New York

This chapter introduces the notion of namespace. We also describe how to manage input and output with C++ commands via the terminal or files.

Standard I/O in C and C++

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

QUESTION BANK. SUBJECT CODE / Name: CS2311 OBJECT ORIENTED PROGRAMMING

Standard Library. Lecture 27. Containers. STL Containers. Standard Library

Chapter 8 File Processing

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

Text File I/O. #include <iostream> #include <fstream> using namespace std; int main() {

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

Getting started with C++ (Part 2)

IBM. C/C++ Legacy Class Libraries Reference SC

Definition Matching (10 Points)

Lecture 4 Tao Wang 1

Chapter-12 DATA FILE HANDLING

C++ basics Getting started with, and Data Types.

Transcription:

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

iostream Two libraries can be used for input and output: stdio and iostream The iostream library is newer and better: It is object oriented It can make use of C++ features such as references, function overloading, and operator overloading It is type safe It is extensible - just overload the << and >> operators for your class Stream I/O 2

Output Streams An ostream converts values into sequences of characters which are then output somewhere Usually the << operator is used to send values to an ostream, but there are also other functions: put Sends a single character cout.put( 'c' ); write Sends a string of characters cout.write( buf, size ); flush Makes sure characters are actually written to wherever the output is going cout.flush( ); Stream I/O 3

Output Streams cont. The predefined ostream object cout sends text to the standard output device (stdout) The ostream object cerr, which is unbuffered, sends text to the standard error device (stderr) Stream I/O 4

Input Streams An istream reads sequences of characters from somewhere and converts them to values The predefined istream object cin reads from the standard input device (stdin) The >> operator is usually used to read values from an istream. By default it skips over white space before reading a value The eof( ) function is used to detect the end of the input file Stream I/O 5

Input Streams cont. Other functions for reading: getline Read an entire line of characters cin.getline( buffer, size ); read Read a string of characters cin.read( buffer, size ); get Read a single character or a string char c = cin.get( ); cin.get( c ); cin.get( buffer, size ); cin.get( buffer, size, delimiter ); Stream I/O 6

Stream Errors There are several functions for detecting errors in an input or output stream: fail True if an error occured. if( cin.fail( ) )... bad True if a fatal error occured. if( cin.bad( ) )... good True if no errors have occured. while( cin.good( ) )... Stream I/O 7

Stream File I/O File stream stuff is in the fstream header: #include <fstream> using namespace std; The header defines several file i/o classes: ofstream Write out to a file ifstream Read in from a file fstream Generic reading/writing with files Stream I/O 8

Stream File I/O cont. There are two ways to open a file: Pass the file name to the constructor: fstream in( mydata ); Use the open function: ofstream out; out.open( names ); When opening a file, a second argument can also be used to specify the open mode: // Open file for appending ofstream fout( data, ios::app ); Other modes include ios::trunc and ios::binary Stream I/O 9

Stream File I/O cont. There are also two ways to check if a file was opened successfully: The overloaded boolean conversion operator: if(!in ) { /* handle error */ } The fail function: if( out.fail( ) )... Once opened, ofstreams can be used just like cout, and ifstreams just like cin Use the close function to close a file: in.close( ); out.close( ); Stream I/O 10

Example (ofstream) An ofstream is used to write to a file: #include <fstream> using namespace std; int main( ) { char * filename = outfile ; ofstream out; out.open( filename ); if(!out ) exit( 1 ); out << Cows go moo << endl; out.close( ); return 0; } Stream I/O 11

Example (ifstream) An ifstream is used to read from a file: #include <fstream> using namespace std; int main( ) { ifstream in( infile ); string s; int n; in >> s; // Read a word from in in >> n; // Read an integer from in in.close( ); return 0; } Stream I/O 12

Formatted Stream Output Output streams have several member functions that are used for formatting The most commonly used ones are: precision( int n ) width( int n ) setf( ios::flag ) Manipulators can also be used for formatting Stream I/O 13

precision Depending on the compiler, precision will set the number of significant digits, or the the number of digits after the decimal point With g++ it is significant digits Output is converted to exponential notation if needed Code: float a = 3.084, b = 26.818; cout.precision( 2 ); cout << a= << a << b= << b << endl; Output: a=3.1 b=26 Stream I/O 14

width width sets the minimum width of the column If more space is needed to print a value, the entire value is printed Calling width affects only the next item that is sent to the output stream Code: int count = 53; cout.width( 10 ); cout << Count= << count << endl; Output (using _ as space): Count=53 Stream I/O 15

setf setf is short for set flag unsetf unsets a flag Some useful ios flags: ios::fixed and ios::scientific If fixed is set, floating point values are not converted to exponential notation If scientific is set, floating point values are always converted to exponential notation Neither of these are set by default Stream I/O 16

setf cont. Useful ios flags continued: ios::showpoint If set, forces the decimal point and trailing zeros to be shown for floating point values If not set, a float with all zeros after the decimal point will look like an integer By default, showpoint is not set ios::showpos If set, forces the + sign to be printed with positive values It is not set by default Stream I/O 17

setf cont. Useful ios flags continued: ios::right When used with width, forces the value to be right justified within the column Automatically unsets left Set by default ios::left When used with width, forces the value to be left justified Automatically unsets right Default is not set Stream I/O 18

Manipulators Manipulators are functions that are called in an unusual way: #include <iomanip>... cout << setw( 10 ) << setprecision( 5 ) << 3.14159265979 << endl; The setw( n )manipulator works the same as ostream.width( n ) Calling setprecision( n ) is the same as calling ostream.precision( n ) Stream I/O 19

Manipulators cont. The familiar manipulator endl is just a function: ostream & endl( ostream & out ) { out << '\n'; // write an end-of-line out.fflush( ); // flush the buffer return out; // return the buffer } A second function is used to call it: ostream & operator<<( ostream & out, ostream & (*fun)(ostream &) ) { return fun( out ); // execute the function } Stream I/O 20