UNIT VI 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 members.

Slides:



Advertisements
Similar presentations
Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
Advertisements

CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.
1 Lecture 31 Handling Selected Topics Again!! File I/O Special Lectures.
File I/O. Stream Objects cin – the standard input stream - an object of the istream class, cout – the standard output stream – an object of the ostream.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Standard.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
1 File I/O 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.
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.
Program Input and the Software Design Process ROBERT REAVES.
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.
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.
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.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
CSE 232: C++ Input/Output Manipulation Built-In and Simple User Defined Types in C++ int, long, short, char (signed, integer division) –unsigned versions.
File I/O ifstreams and ofstreams Sections 11.1 &
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.
FILE HANDLING IN C++.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
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:
1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.
CSE 332: C++ IO We’ve Looked at Basic Input and Output Already How to move data into and out of a program –Using argc and argv to pass command line args.
4. Input/Output Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 C++ Input/Output: Streams The.
Student Book Input / Output in C++
 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.
1 Today’s Objectives  Announcements Turn in Homework 4 Quiz 4 will be on Wednesday, July 19 – It will have questions about inheritance, polymorphism,
1 Simple File I/O Chapter 11 Switch Statement Chapter 12.
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 13: Inheritance and Composition.
C++ REVIEW INPUT/OUTPUT (I/O). BRIEF NOTE “CLASSES” AND “STRUCTS” AND “TYPES” They are ADTs… They define data and operations on that data The ADTs in.
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 CSC241: Object Oriented Programming Lecture No 32.
Streams One of the themes of this course is that everything can be reduced to simple (and similiar) concepts. Streams are one example. Keyboard and Screen.
Input/Output. Objectives In this chapter you will: Learn what a stream is and examine input and output streams Explore how to use the input stream functions.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
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.
Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February IO Streams  IOStreams are part of the Standard C++ library.
CSCI 333 Data Structures I/O with C++ 20 October, 2003.
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
Streams and IO  Streams are pipe like constructors used for providing IO.  When a programmer needs to handle input from or output to external entities,then.
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
CS212: Object Oriented Analysis and Design
Hank Childs, University of Oregon
Standard C++ Input/Output and String Classes
Output Stream Formatting
Standard Input/Output Streams
Standard Input/Output Streams
Advanced Input and Output
Programming with Data Files
Chapter 3: Input/Output
Managing console i/o operations
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
Introduction to Programming
Programming with ANSI C ++
File I/O.
ENERGY 211 / CME 211 Lecture 9 October 10, 2008.
Presentation transcript:

UNIT VI 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 members functions, overloading the extractions & insertion operators, Memory as a stream object, command- line arguments, Persistent Objects

Stream Classes  A stream is a general name given to a flow of data.  In C++ a stream is represented by an object of a particular class.  Example - cin and cout are stream objects.  Different streams are used to represent different kinds of data flow. For example, the ifstream class represents data flow from input disk files. Advantages of Streams Simple to Use. There are no formatting characters (such as %d ) in streams, since each object already knows how to display itself. This removes a major source of errors. Existing operators and functions can be overloaded. For Ex - the insertion ( >) operators. This makes your own classes work in the same way as the built-in types, which again makes programming easier and more error free.

Stream Class Hierarchy

The ios Class The ios class is the granddaddy of all the stream classes, and contains the majority of the features to operate C++ streams. The three most important features are – 1.Formatting flags 2.Error-status flags 3.File operation mode. 1.Formatting Flags Formatting flags are a set of enum definitions in ios. They act as on/off switches that specify choices for various aspects of input and output format and operation.

Manipulators Manipulators are formatting instructions inserted directly into a stream. Examples – 1.Endl manipulator sends a newline to the stream. cout << “To each his own.” << endl; 2. setiosflags() manipulator cout << setiosflags(ios::fixed) // use fixed decimal point << setiosflags(ios::showpoint) // always show decimal point << var;

Functions The ios class contains a number of functions that you can use to set the formatting flags and perform other tasks.

Examples - cout.width(14); cout.fill(‘*’); cout.setf(ios::left); cout.unsetf(ios::left);

The istream Class The istream class, which is derived from ios, performs input-specific activities, or extraction.

The ostream Class The ostream class handles output or insertion activities.

Stream Errors Error-Status Bits The stream error-status flags constitute an ios enum member that reports errors that occurred in an input or output operation.