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.

Slides:



Advertisements
Similar presentations
File I/O in C++. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk);is stored on a secondary storage device.
Advertisements

1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
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.
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.
Chapter 3: Input/Output
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 INPUT OUTPUT. Topics to be discussed……………….. overloaded operators >> and and
1 Advanced Input and Output COSC1567 C++ Programming Lecture 9.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
C++ Lecture 8 Monday, 25 August I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing.
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
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.
1 CS161 Introduction to Computer Science Topic #13.
FILE HANDLING IN C++.
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
I/O in C++ October 7, Junaed Sattar. Stream I/O a stream is a flow of bytes/characters/ints or any type of data input streams: to the program output.
Chapter 9 Streams: Input stream: source of characters. Output stream: destination for characters. Up to now the input stream has defaulted to the keyboard.
FILE I/O IN C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Basics Outline 21.1Introduction 21.2Streams Iostream Library.
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 -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.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
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.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
Program Input and the Software Design Process ROBERT REAVES.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
GE 211 Programming in C ++ Dr. Ahmed Telba Room :Ac -134
Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February IO Streams  IOStreams are part of the Standard C++ library.
Streams and File Processing in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
File I/O in C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
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.
Computer Programming II Lecture 9. Files Processing - We have been using the iostream standard library, which provides cin and cout methods for reading.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني I/O and File management Concept of streams. cin and cout objects. C++stream classes. Unformatted I/O.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
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
Chapter 14: Sequential Access Files
EGR 2261 Unit 3 Input/Output Read Malik, Chapter 3.
Basic Input and Output Operations
DATA HANDLING.
STREAM HANDLING.
Basic File I/O and Stream Objects
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
Chapter 3: Input/Output
Topics Input and Output Streams More Detailed Error Testing
Sindhu S PGT Comp.Sc. K V Kollam
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
File I/O in C++ I.
File I/O.
C++ Programming Basics
File I/O in C++ I.
Programming Fundamental-1
Presentation transcript:

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. Input Stream – flow of data from the file to program variables. - keyboard is the source of input stream. Output Stream – flow of data to a file from program variables. - The monitor is also treated as a target for output stream. | Website for Students | VTU NOTES

The Class Hierarchy for Handling Streams – C++ provides a library of classes that have functionality to implement various aspects of stream handling. Fig – Library of Classes that handle streams These classes are arranged in hierarchical fashion using inheritance ios istream ostream ifstream iostreamofstream Ostream_withassign fstreamIstream_withassign | Website for Students | VTU NOTES

The class ios is at the base class in this hierarchy. Class ostream is derived from the class ios and handles the general o/p stream. 1.The insertion operator << is defined and overloaded in the class ostream to handle output streams from the program variables to the output files. 2.Class ostream_withassign is derived from the class ostream. 3.Cout is an object of class ostream_withassign and stands for console output 4.C++ treats all peripheral devices as files. 5.Treats monitor as file for o/p file 6.The object cout represents monitor | Website for Students | VTU NOTES

Hence statement cout << x; means insert the stream from program variable x into a file called cout (monitor) Class istream is derived from ios and handles general i/p streams. Extraction operator (>>) is defined and overloaded in the class istream to handle input streams from i/p files to program variables. The class istream_withassign is derived from class istream. cin is an object of class stream_withassign and stands for console o/p. C++ treats all peripheral devices as files. Treats the keyboard also as a file. Object cin represents the keyboard. Statement cin >> x; Means “extract stream from file and place it in the program vriable x. Class of ofstream is derived from the class ostream. It has functionality to Handle o/p streams to disk files. | Website for Students | VTU NOTES

Objects of class ofstream represent o/p files on the disk. Code opens a disk file for o/p ofstream ofile (“first.dat”); First.dat is opened for o/p in the directory where the executable will be run. Entire path of file to be opened can also be prefixed to the name of the file. The object ofile can be passed as lhs operand instead of cout. ofile << x; “Insert the stream from file from program variable x into the file ‘first.dat’.” The class ifstream is derived from class istream. Ifstream handles i/p streams from disks. Code opens the disk file for input. ifstream ifile(“first.dat”); File is opened for input in the directory where the executable file is run. Entire path of the file to be opened can also be prefixed to the name of the file. | Website for Students | VTU NOTES

Since the extraction operator is defined in the base class of the class ifstream, the object ifile can be passed as lhs operand instead of cin. ifile << x; This statement translates as “ Extract the stream from the file ‘first.dat’ and place it in a program variable x. The class fstream is derived from the class iostream. It handles both input and output files from and to disk files. The classes for handling streams to and from disk files are defined in the header file fstream.h. The classes for handling general streams are defined in the header file iostream.h and this is included in fstream.h | Website for Students | VTU NOTES

How Data is stored in memory ? Text and Binary Input – During runtime, the value of character variable is stored in memory as binary equivalent of its ASCII equivalent. But value of int, float, char or double type variable is stored as its binary equivalent.d Eg – if value of character type variable A, it is stored in 1 byte where the bits represent the number 65 i.e. ascii equivalent of base but if int var is 65, is stored in 4 bytes where bits represent 65 in base 2. All other datatypes are stored in similar fashion | Website for Students | VTU NOTES

Input/Output of character Data – There is no difference between text mode and binary mode. In both modes, the value of character data type is copied from memory to output file as it is. Input/Output of Numeric data – A standard library function will read value of source variable in the memory, needs to be output in the base 10(text format). It hence reads the value of source variable from the memory, transform the representation of data from existing base 2 to base 10 and only then copy it to the output file. standard library function inputs numeric data in binary mode will reckons i.e. the data that is in base 2(binary) format. It will read the vale from memory and copy it into target variable in memory. | Website for Students | VTU NOTES

Text vs Binary Files - In text files, binary data (numeric data i.e. stored in base 2 format in memory) is stored in base 10 format. In binary files, the same binary data is stored in the same format (base 2). The C++ standard library provides functions that read input from from files and write the read values into variables in binary mode. These functions require the address of variable whose data needs to be input along with its size. | Website for Students | VTU NOTES

Text Input / Output – Text output is achieved in C++ by : The Insertion operator The put( ) function | Website for Students | VTU NOTES

The Insertion operator – can be used to output values to the disk files. IO outputs in the text mode. Io has been defined and overloaded in class ostream. It takes an object of class ‘ostream’ as its lhs operand. Rhs operand takes a value of 1 of the datatypes. It copies the value on its right into file i.e. associated with the object on its left. | Website for Students | VTU NOTES

Charfileoutput.cpp #include Void main() {char cVar; Ofstream ofile(“first.dat”); cVar = ‘A’; Ofile << cVar; } Outputting a character in text mode by using the insertion operator Last statement copies value of cvar from memory to disk file first.dat without changing its representation in any way. | Website for Students | VTU NOTES

Inserting integers into o/p streams using the insertion operator. An int type value occupy 4 bytes in the memory. If output in text mode, by insertion operator, the number of bytes it occupies in the output file depends upon its value. | Website for Students | VTU NOTES

Include void main() { Int ivar; Ofstream ofile(“first.dat”); Ivar=111; Ofile << ivar; } Outputting an integer in text mode by using insertion operator. | Website for Students | VTU NOTES

Action on data of different types. Insertion operator has been overloaded differently for each of data types as follows. | Website for Students | VTU NOTES

Insertion operator has been overloaded differently for each of data types as follows. 1.Inserting characters into output streams using the insertion character. Eg – program for outputting a character in text mode by using insertion operator 2.Inserting integers into output streams using the insertion characters. Eg – program for outputting a integer in text mode by using the insertion operator 3.Inserting floats and doubles into output streams using the insertion operator. Eg – program for outputting a float in text mode by using insertion operator | Website for Students | VTU NOTES

4.Inserting strings into output streams using the insertion operator. Eg – program for outputting a string in text mode by using the insertion operator. 5.Inserting objects into output streams using the insertion operator. | Website for Students | VTU NOTES

Put() function – is a member of the ostream class. Prototype - ostream & ostream :: put(char c); Function can be called with respect to an object of the ostream class / any class derived from the ostream class. One such object is cout. Function copies the character i.e. passed as a parameter to it into the o/p file associated with object w.r.t. which the function is called. | Website for Students | VTU NOTES

Text Input – is achieved in C++ by: Extraction Operator function, get() function and getline() Extraction Operator - can be overloaded differently for each of the datatypes as follows. 1.Extracting characters from the input streams using the extraction operator. Eg – program for inputting a character in text mode by using the extraction operator. 2.Extracting integers from the input streams using the extraction operator. Eg – program for inputting a integer in text mode by using the extraction operator. | Website for Students | VTU NOTES

3.Extracting floats and doubles from input streams using the extraction operator. 4.Extracting strings from the input streams using the extraction operator. Eg – program for inputting a character array by using the extraction operator. 5.Extracting objects from the input streams using the extraction operators. Eg – program for inputting a character by using the get() function. | Website for Students | VTU NOTES

Getline() Function – reads one line from the i/p file, it has been defined in the class istream. Prototype – istream & istream :: getline(char*,int, Char = ‘\n’); | Website for Students | VTU NOTES

Binary Input / Output Binary Output – write() Function –copies the value of the variables from the memory to the specified memory location. – Binary mode functions are interested in the address of the variables (from point of block whose data needs to be output) and the size of the variable (total number of bytes to be output). – Prototype – Ostream & ostream :: write(const char*, int); – Write() function is defined in ostream taking 2 parameters. | Website for Students | VTU NOTES

1.Address of the variable whose value is need to to be outputted. 2.Size of the variable. Write() function is used to output the data of various types. 1.Inserting characters into the output streams using write() function 2.Inserting integers into the output streams using write() function 3.Inserting floats and doubles into the output streams using write() function 4.Inserting strings into the output streams using write() function 5.Inserting objects into the output streams using write() function | Website for Students | VTU NOTES

Binary Input – Read() Function – copies values from the specified i/p file to memory block i.e. occupied by target variable. It works in binary mode. Prototype – Istream & istream :: read(char *, int); Takes 2 parameters 1.Address of variable into which read value needs to be input. 2.Size of the variable. Read() is used to input the data of various types | Website for Students | VTU NOTES

Read() is used to input the data of various types 1.Extracting characters for the input streams using the read() function 2.Extracting integers for the input streams using the read() function 3.Extracting float and doubles from the input streams using the read() function 4.Extracting strings from the input streams using the read() function 5.Extracting objects from the input streams using the read() function | Website for Students | VTU NOTES

Opening and Closing Files | Website for Students | VTU NOTES