26
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. www.bookspar.com | Website for Students | VTU NOTES

Stream Handling

  • Upload
    lenore

  • View
    42

  • Download
    0

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Stream Handling

www.bookspar.com | Website for Students | VTU NOTES

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.

Page 2: Stream Handling

www.bookspar.com | 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 iostream ofstream

Ostream_withassignfstreamIstream_withassign

Page 3: Stream Handling

www.bookspar.com | 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 ostreamto 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

Page 4: Stream Handling

www.bookspar.com | Website for Students | VTU NOTES

Hence statement cout << x;meansinsert 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 toHandle o/p streams to disk files.

Page 5: Stream Handling

www.bookspar.com | Website for Students | VTU NOTES

Objects of class ofstream represent o/p files on the disk.Code opens a disk file for o/pofstream 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 streamsfrom 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. Entirepath of the file to be opened can also be prefixed to the name of the file.

Page 6: Stream Handling

www.bookspar.com | 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

Page 7: Stream Handling

www.bookspar.com | 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 2.

01000001 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.01000001000000000000000000000000

Page 8: Stream Handling

www.bookspar.com | 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 willread value of source variable in the memory , needs to be outputin 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 base10 and only then copy it to the output file.standard library function inputs numeric data in binary mode willreckons i.e. the data that is in base 2(binary) format.It will read the vale from memory and copy it into target variablein memory.

Page 9: Stream Handling

www.bookspar.com | 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.

Page 10: Stream Handling

www.bookspar.com | Website for Students | VTU NOTES

Text Input / Output – Text output is achieved in C++ by :

• The Insertion operator• The put( ) function

Page 11: Stream Handling

www.bookspar.com | 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.

Page 12: Stream Handling

www.bookspar.com | Website for Students | VTU NOTES

Charfileoutput.cpp#include <iostream.h>Void main(){char cVar;Ofstream ofile(“first.dat”);cVar = ‘A’;Ofile << cVar;}Outputting a character in text mode by using the insertion operatorLast statement copies value of cvar from memory to diskfile first.dat without changing its representation in anyway.

Page 13: Stream Handling

www.bookspar.com | 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.

Page 14: Stream Handling

www.bookspar.com | Website for Students | VTU NOTES

Include <fstream.h>void main()

{Int ivar;Ofstream ofile(“first.dat”);Ivar=111;Ofile << ivar;}Outputting an integer in text mode by usinginsertion operator.

Page 15: Stream Handling

www.bookspar.com | Website for Students | VTU NOTES

• Action on data of different types.Insertion operator has been overloaded differently for each of data types as follows.

Page 16: Stream Handling

www.bookspar.com | 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

Page 17: Stream Handling

www.bookspar.com | 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.

Page 18: Stream Handling

www.bookspar.com | 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.

Page 19: Stream Handling

www.bookspar.com | 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.

Page 20: Stream Handling

www.bookspar.com | 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.

Page 21: Stream Handling

www.bookspar.com | 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’);

Page 22: Stream Handling

www.bookspar.com | 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 .

Page 23: Stream Handling

www.bookspar.com | 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.3. Inserting characters into the output streams using

write() function4. Inserting integers into the output streams using

write() function5. Inserting floats and doubles into the output streams

using write() function6. Inserting strings into the output streams using

write() function7. Inserting objects into the output streams using

write() function

Page 24: Stream Handling

www.bookspar.com | 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 parameters1. 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

Page 25: Stream Handling

www.bookspar.com | Website for Students | VTU NOTES

Read() is used to input the data of various types1. Extracting characters for the input streams using

the read() function2. Extracting integers for the input streams using

the read() function3. Extracting float and doubles from the input

streams using the read() function4. Extracting strings from the input streams using

the read() function5. Extracting objects from the input streams using

the read() function

Page 26: Stream Handling

www.bookspar.com | Website for Students | VTU NOTES

• Opening and Closing Files