Object Oriented Programming Unit-4a Basic Theory

  1. Concept of Streams,
  2. cin and cout Objects,
  3. C++ Stream Classes,
  4. Unformatted and Formatted I/O,
  5. Manipulators,
  6. File Stream,
  7. C++ File Stream Classes,
  8. File Management Functions,
  9. File Modes,
  10. Binary and Random Files.

C++ comes with libraries, which provide us with many ways for performing input and output.

In C++ input and output is performed in the form of a sequence of bytes or more commonly known as streams.

Input Stream: If the direction of flow of bytes is from the device (for example, Keyboard) to the main memory then this process is called input.

Output Stream: If the direction of flow of bytes is opposite, i.e. from main memory to device ( display screen ) then this process is called output.

  1. iostream: iostream stands for standard input-output stream. This header file contains definitions to objects like cin, cout, cerr etc.
  2. iomanip: iomanip stands for input output manipulators. The methods declared in this files are used for manipulating streams. This file contains definitions of setw, setprecision etc.
  3. fstream: This header file mainly describes the file stream. This header file is used to handle the data being read from a file as input or data being written into the file as output.

  • Generally in C programming language, printf and scanf are considered to be functions.
  • When it comes to cout and cin, in C++ what are they?
  • It mean they can’t be functions as they are not followed by parenthesis, so they are not functions.
  • So what are cout and cin? Are they standard input and output functions? OR something else?
  • std::cout and std::cin are global objects of classes std::ostream and std::istream respectively, which they've overloaded operator << and >>.
  • The predefined object cout is an instance of ostream class.
  • The cout object is said to be "connected to" the standard output device, which usually is the display screen. The cout is used in conjunction with the stream insertion operator, which is written as << which are two less than signs as shown in the following
  • #include<iostream.h>
    void main()
    {
    int A=10,b=20;
    cout<<"A="<<Aendl<<"B="<<B;
    }
  • The insertion operator << may be used more than once in a single statement as shown above and endl is used to add a new-line at the end of the line.
  • The predefined object cin is an instance of istream class.
  • The cin object is said to be attached to the standard input device, which usually is the keyboard.
  • The cin is used in conjunction with the stream extraction operator, which is written as >> which are two greater than signs as shown in the following example.
  • #include <iostream.h>
    void main()
    {
    char name[50];
    cout << "Please enter your name: ";
    cin >> name;
    cout << "Your name is: " << name << endl;
    }

C++ provides standard iostream library to operate with streams.

The iostream is an object-oriented library which provides Input/Output functionality using streams.
In the above figure, ios is the base class.

The iostream class is derived from istream and ostream classes.

The ifstream and ofstream are derived from istream and ostream, respectively. These classes handles input and output with the disk files.

The fstream.h header file contains a declaration of ifstream, ofstream and fstream classes.

The iostream.h file contains istream, ostream and iostream classes and included in the program while doing disk I/O operations.

Stream classes have good error handling capabilities.

These classes work as an abstraction for the user that means the internal operation is encapsulated from the user.

These classes are buffered and do not uses the memory disk space.

These classes have various functions that make reading or writing a sequence of bytes easy for the programmer.

The printed data with default setting by the I/O function of the language is known as unformatted data.

It is the basic form of input/output and transfers the internal binary representation of the data directly between memory and the file.

For example, in cin statement it asks for a number while executing. If the user enters a decimal number, the entered number is displayed using cout statement. There is no need to apply any external setting, by default the I/O function represents the number in decimal format.

In C++, we can read the input entered by a user at console using an object cin of istream class and through this object we can access the functions of istream class, such as - get(char *), get(void) and getline().

In C++, we can write the output at console using an object cout of ostream class and through this object we can access the functions of ostream class, such as - put(), write().

Using getline(istream& is, string& str) function

If the user needs to display a number in hexadecimal format, the data is represented with the manipulators are known as formatted data.

For example,
cout<<hex<<13;
converts decimal 13 to hexadecimal

Formatting is a representation of data with different settings (like number format, field width, decimal points etc.) as per the requirement of the user.

If the user needs to display a numbers like 1,11,111 in following format,
**1
*11
111

Then the data is represented with the manipulators are known as formatted data.

  • setw (val): It is used to sets the field width in output operations.
  • setfill (c): It is used to fill the character ‘c’ on output stream.
  • setprecision (val): It sets val as the new value for the precision of floating-point values.
  • setbase(val): It is used to set the numeric base value for numeric values.
  • setiosflags(flag): It is used to sets the format flags specified by parameter mask.
  • resetiosflags(m): It is used to resets the format flags specified by parameter mask.

showpos: It forces to show a positive sign on positive numbers.
noshowpos: It forces not to write a positive sign on positive numbers.
showbase: It indicates numeric base of numeric values.
uppercase: It forces uppercase letters for numeric values.
nouppercase: It forces lowercase letters for numeric values.
fixed: It uses decimal notation for floating-point values.
scientific: It use scientific floating-point notation.
hex: Read and write hexadecimal values for integers and it works same as the setbase(16).
dec: Read and write decimal values for integers i.e. setbase(10).
oct: Read and write octal values for integers i.e. setbase(10).
left: It adjust output to the left.
right: It adjust output to the right.

• C/C++ IO are based on streams, which are sequence of bytes flowing in and out of the programs.

• In input operations, data bytes flow from an input source (such as keyboard, file, network or another program) into the program.

• In output operations, data bytes flow from the program to an output sink (such as console, file, network or another program).

• Streams acts as an intermediaries between the programs and the actual IO devices, in such the way that frees the programmers from handling the actual devices, so as to archive device independent IO operations.

File Handling concept in C++ language is used for store a data permanently in computer. Using file handling we can store our data in Secondary memory (Hard disk). Why use File Handling in C++ • For permanent storage. • The transfer of input - data or output - data from one computer to another can be easily done by using files.

For read and write from a file you need another standard C++ library called fstream, which defines three new data types:

For achieving file handling in C++ we need follow following steps

• Naming a file
• Opening a file
• Reading data from file
• Writing data into file
• Closing a file

The function open() can be used to open multiple files that use the same stream object.

Syntax
file-stream-class stream-object;
stream-object.open("filename");


Example
ofstream outfile; // create stream
outfile . open ("data1"); // connect stream to data1

For Example:
fstream file;
file.Open("data . txt", ios :: out | ios :: in);

Each file have two associated pointers known as the file pointers.

One of them is called the input pointer (or get pointer) and the other is called the output pointer (or put pointer).

The input pointer is used for reading the contents of a given file location and the output pointer is used for writing to a given file location.

A file must be close after completion of all operation related to file.

For closing file we need close() function.

Syntax
outfile.close();

When we want to move file pointer to desired position then use these function for manage the file pointers.

The function put() write a single character to the associated stream.

Similarly, the function get() reads a single character from the associated stream.

These function take two arguments. The first is the address of the variable V , and the second is the length of that variable in bytes.

The address of variable must be cast to type char * (i.e pointer to character type).

For Example
file.read ((char *)&V , sizeof (V));
file.write ((char *)&V , sizeof (V));