Home » Learn C++ » Streams in C++

Streams in C++

Streams and Files:

            Stream is a general term. It refers to flow of data from a source to a destination. The process of inputting data from the source is known as reading, extracting, getting or fetching. The process of outputting data to the destination is known as writing, inserting, putting or storing.

            In C++ special classes known as stream classes are used to handle data streams. These classes are called stream classes. Stream classes are defined in header files and these are included in the program, through their header files. The objects of stream classes are used to control flow of data.

            For example, in C++ programs we commonly include the header file “iostream.h”. This header file contains the objects “cin”, cout”, “cerr” and “clog”. These objects arc used to move streams of data across various parts of the computer.

 Following are the most commonly used C++ streams classes:

 ifstream This stream is used to perform input operations on data tiles on the disk. The objects of this stream are used to read data from a tile on disk into the computer memory.

ofstream This stream is used to perform output operations on data files on the disk. The objects of this stream are used to write data from the computer memory into a file on the disk.

fstream This stream is used for both input & output operations on tiles on the disk.

 The extraction operator “>>” that is used for input operations is a member of the istream class. The insertion operator ”>>”that is used for output operations is a member of the ostream class. Both istream & ostream classes arc derived from the ios class.
The “ios” class is the base stream class. All other stream classes are derived from this class. The following figure shows hierarchy of stream classes:

streams-files

             In C++ programs, the header files “fstream.h” must be included into the program. The “ifstream”, “ofstream” and “fstream” classes are defined in this header file. These classes contain some member functions that are used to perform input and output operations on disk files. The objects of these classes can be created and the data files are associated with these objects. The member functions of these classes are then used to process data in tiles on the disk.


Other Topics

comment closed

Get Adobe Flash playerPlugin by wpburn.com wordpress themes
Copyright © 2010 Information Village. All rights reserved.