Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

21 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’); www.bookspar.com | Website for Students | VTU NOTES

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

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

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

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

26 Opening and Closing Files www.bookspar.com | Website for Students | VTU NOTES


Download ppt "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."

Similar presentations


Ads by Google