OOP Using C++ – C++ Programming IO and File Management based Questions and Answers

Lists of Questions and Answers

  • 1. List out C++ Stream Classes.
  • 2. List out and explain Unformatted I/O Operations
  • 3. List out and explain functions and manipulators used for Formatted I/O operations.
  • 4. Explain file stream classes with iostream classes in short.
  • 5. List out various File mode parameters and explain in short.
  • 6. Explain File pointing functions in short.
  • 7. Write a Program for reading and writing class objects.
  • 8. Write a Program that write content of file1 in file2 but omits uppercase letters.

 

Question – 1. List out C++ Stream Classes.

ios (General I/O stream class)

  • Contains basic facilities that are used by all other input and output classes.
  • Also contains a pointer to a buffer object.
  • Declares constants and functions that are necessary for handling formatted input and output functions.

 

istream (Input stream)

  • Inherits the properties of ios.
  • Declares input functions such as get(), getline() and read()
  • Contains overloaded extraction operator >>

 

ostream (output stream)

  • Inherits the properties of ios.
  • Declares output functions such as put() and write()
  • Contains overloaded insertion operator <<

 

iostream (I/O stream)

  • Inherits the properties of ios, istream and ostream through multiple inheritance and
  • thus contains all the input and output functions.

 

streambuf

  • Provides an interface to physical devices through buffers.
  • Acts as a base for filebuf class used ios files.

 

 

Question – 2. List out and explain Unformatted I/O Operations.

 

  • C++ language provides a set of standard built-in functions which will do the work of reading and displaying data or information on the I/O devices during program execution.
  • Such I/O functions establish an interactive communication between the program and user.

 

Function Syntax Use
cout cout<<” ”<<” ”; To display character, string and number on output device.
cin cin>> var1>>var2; To read character, string and number from input device.
get(char*) char ch; To read character including blank space, tab and newline character from input device.
cin.get(ch); It will assign input character to its argument.
get(void) char ch;

ch=cin.get();

To read character including blank space, tab and newline character from input device.

 

It will returns input character.

put() char ch;

cout.put(ch);

To display single character on output device.

 

If we use a number as an argument to the function put(), then it will convert it into character.

getline() char name[20];

int size=10;

cin.getline(name,size);

It is used to reads a whole line of text that ends with a

newline character or size -1 character.

First argument represents the name of string and second argument indicates the number of character to be read.

 

write() char name[20];

int size=10; cout.write(name,size);

It is used to display whole line of text on output device.

 

First argument represents the name of string and second argument indicates the number of character to be display.

 

 

Example of cin and cout:

#include <iostream>
using namespace std;
int main()
{

   int a;

   cout<<"Enter the number";

   cin>>a;

   cout<<"The value of a="<<a;

   return 0;
}

 

 

Example of get(char*), char(void) and put():

#include <iostream>
using namespace std;
int main()
{
    int a=65;
    char ch;
    cin.get(ch); //get(char*)
    cout.put(ch); //put()
    ch=cin.get(); //get(void)
    cout.put(ch);
    cout.put(a);
    return 0;

}

 

 

Example of getline() and write():

#include <iostream>

using namespace std;

int main()

{

int size=5;

char name[50];

cin.getline(name,size); //getline()

cout.write(name,size); //write

return 0;

}

 

 

Question – 3. List out and explain functions and manipulators used for Formatted I/O operations.

 

We can format input and output by following methods.

  1. ios class funstions and flags.
  2. Manipulators.
  3. User-defined output functions.

 

Now we will see each method in detail.

The ios format functions are shown in below table:

Function Syntax Use
width() cout.width(size); To specify the required field size for displaying an output value.
precision() cout.precision(2); To specify the number of digits to be displayed after the decimal point of a float value.
fill() cout.fill(‘character’); To specify a character that is used to fill the unused portion of a field.
setf() cout.setf(arg1, arg2); To specify format flags that can control the form of output display such as left or right justification.
unsetf() cout.resetiosflags() To clear the flags specified.

 

In setf() we can provide one or two argument.

cout.setf(arg1, arg2);

The arg1 is formatting flags defined in the ios class. And arg2 is known as bit field specifies the group to which the formatting flags belong.

 

The flags and bit field are shown below

Format required Flag (arg1) Bit-field (arg2)
Left justified output ios::left ios::adjustfield
Right justified output ios::right ios::adjustfield
Padding after sign or base indicator (like +##20) ios::internal ios::adjustfield
Scientific notation ios::scientific ios::floatfield
Fixed point notation ios::fixed ios::floatfield
Decimal base ios::doc ios::basefield
Octal base ios::oct ios::basefield
Hexadecimal base ios::hex ios::basefield

 

The flags without field bit are shown below

Flag Meaning
ios::showbase Use base indicator on output.
ios::showpos Print + before positive numbers.
ios::showpoint Show trailing decimal point and zeros.
ios::uppercase Use uppercase letters for hex output.
ios::skipus Skip white space on input.
ios::unitbuf Flush all streams after insertion.
ios::stdio Flush stdout and stderr after insertion.

 

Example of ios functions:

#include <iostream>

#include <math>

using namespace std;

int main()

{

cout.fill('*');

cout.setf(ios::left,ios::adjustfield);

cout.width(10);

cout<<"value";

cout.setf(ios::right,ios::adjustfield);

cout.width(15);

cout<<"SQRT OF VALUE"<<"\n";

cout.fill('.');

cout.precision(4);

cout.setf(ios::showpoint);

cout.setf(ios::showpos);

cout.setf(ios::fixed,ios::floatfield);

for(int i=1;i<=10;i++)

{

cout.setf(ios::internal, ios::adjustfield);

cout.width(5);

cout<<i;

cout.setf(ios::right, ios::adjustfield);

cout.width(20);

cout<<sqrt(i)<<"\n";

}

cout.setf(ios::scientific, ios::floatfield);

cout<<"\nSQRT(100)="<<sqrt(100)<<"\n";

return 0;

}

 

 Output:

value*******SQRT OF VALUE

+…1………….+1.0000

+…2………….+1.4142

+…3………….+1.7321

+…4………….+2.0000

+…5………….+2.2361

+…6………….+2.4495

+…7………….+2.6458

+…8………….+2.8284

+…9………….+3.0000

+..10………….+3.1623

SQRT(100)=+1.0000e+01

 

The manipulators are shown in below table:

 

Manipulators Use
setw() To specify the required field size for displaying an output value.
setprecision() To specify the number of digits to be displayed after the decimal point of a float value.
setfill() To specify a character that is used to fill the unused portion of a field.
setiosflags() To specify format flags that can control the form of output display such as left or right justification.
resetiosflags() To clear the flags specified.

 

Manipulators are used to manipulate the output in specific format.

Example for manipulators

#include <iostream>

#include <iomanip>

using namespace std;

int main()

{

cout.setf(ios::showpoint);

cout<<setw(5)<<"n"

<<setw(15)<<"Inverse of n"

<<setw(15)<<"Sum of terms\n\n";

double term,sum=0;

for(int n=1;n<=10;n++)

{

term=1.0/float(n);

sum=sum+term;

cout<<setw(5)<<n

<<setw(14)<<setprecision(4)

<<setiosflags(ios::scientific)<<term

<<setw(13)<<resetiosflags(ios::scientific)

<<sum<<endl;

}

return 0;

}

 Output:

n Inverse of n Sum of terms

1 1.0000e+00 1.0000

2 5.0000e-01 1.5000

3 3.3333e-01 1.8333

4 2.5000e-01 2.0833

5 2.0000e-01 2.2833

6 1.6667e-01 2.4500

7 1.4286e-01 2.5929

8 1.2500e-01 2.7179

9 1.1111e-01 2.8290

10 1.0000e-01 2.9290

 

Question – 4. Explain file stream classes with iostream classes in short.

 

filebuf

Its purpose is to set the file buffers to read and write.

Contains Openprot constant used in the open() of file stream classes. Also contain close() and open() as members.

 

fstreambase

Provides operations common to the file streams serves as a base for fstream, ifstream and ofstream classes.

Contains open() and close() functions.

 

ifstream

Provides input operations. Contains open() with default input mode. Inherits the functions get(), getline(),

read(), seekg() and tellg() functions from istream.

 

ofstream

Provides output operations. Contains open() with default output mode. Inherits put(), seekg(), tellp(),

And write(), functions from ostream.

 

fstream

Provides support for simulations input and output operations. Contains open() with default input mode.

Inherits all the functions from istream and ostream classes through iostream.

 

Question – 5. List out various File mode parameters and explain in short.

Parameter Meaning
ios::app Append to end of file.
ios::ate Go to end of file on opening.
ios::binary Binary file.
ios::in Open file for reading only.
ios::nocreate Open fails if the file does not exist.
ios::noreplace Open file if the file already exists.
ios::out Open file for writing only.
ios::trunc Delete the contents of the file it it exists

 

  • Opening a file in ios::out mode also opens it in the ios::trunc mode by default.
  • Both ios::app and ios::ate take us to the end of the file when it opened. The difference between the two parameters is that the ios::app allows us to add data to the end of file only, while ios::ate mode permits us to add data or modify the existing data anywhere in the file. In both the cases, a file is created by the specified name, if it does not exist.
  • Creating a stream using ifstream implies input and creating a stream using ofstream implies output. So, in these cases it is not necessary to provide the mode parameters.
  • The mode can combine two or more parameters using the bitwise OR operator shown as follows.

Fout.open(“data”, ios::app | ios::nocreate);

This opens the file in the append mode but fails to open the file if it does not exist.

 

Question – 6. Explain File pointing functions in short.

Each file has two pointers one is getpointer to input and second one is putpointer to output.

 

Functions for manipulation of file pointer

Functions Meaning
seekg() Moves get pointer (input) to specified location.
seekp() Moves put pointer (output) to specified location.
tellg() Gives the current position of the get pointer.
tellp() Gives the currint position of the put pointer.

 

For example:

infile.seekg(20);

int p=fileout.tellp();

We can also pass two argument in the seekg() and seekp() functions as below.

seekg(offset, refposition);

seekp(offset, refposition);

 

The parameter offser represent the number of bytes the file pointer is to be moved from the location

specified by the parameter refposition.

For refposition the ios class provides following constants.

ios::beg – Starting of the file.

ios::cur – Current position of the pointer.

ios::end – End of the file.

 

Example:

#include<iostream>

#include<fstream>

#include<cstring>

using namespace std;

class emp

{

char name[30];

int ecode;

public:

emp()

{

}

emp(char *c, int c)

{

strcpy(name,n);

ecode=c;

}

};



void main()

{

emp e[4];

e[0]=emp(“amit”,1);

e[1]=emp(“joy”,2);

e[2]=emp(“rahul”,3);

e[3]=emp(“vikas”,4);

fstream file;

file.open(“empolyee.dat”, ios::in | ios::out);

int i;

for(i=0;i<4;i++)

file.write((char *) &e[i], sizeof(e[i]));

file.seekg(0, ios::end);

int end=file.tellg();

cout<<”number of objecs stored in employee.dat is”<<sizeof(emp);

}

 

Question – 7. Write a Program for reading and writing class objects.

 

#include<iostream>

#include<fstream>

using namespace std;

class inventory

{

char name[10];

int code;

float cost;

public:

void readdata();

void writedata();

};

void inventory::readdata()

{

cout<<"Enter name"<<endl;

cin>>name;

cout<<"Enter code"<<endl;

cin>>code;

cout<<"Enter price/cost"<<endl;

cin>>cost;

}

void inventory::writedata()

{

cout<<"Name ="<<name;

cout<<"Name ="<<code;

cout<<"Name ="<<cost;

}

int main()

{

inventory item[3];

fstream file;

file.open("stock.txt",ios::in |ios::out);

cout<<"Enter details of 3 items";

for(int i=0;i<3;i++)

{

item[i].readdata();

file.write((char *)&item[i], sizeof(item[i]));

}

file.seekg(0);

cout<<"output";

for(int i=0;i<3;i++)

{

file.read((char *)&item[i],sizeof(item[i]));

item[i].writedata();

}

file.close();

return 0;

}

 

Question – 8.  Write a Program that write content of file1 in file2 but omits uppercase letters.

 

#include<iostream>

#include<fstream>

using namespace std;

int main()

{

fstream f1,f2;

f1.open("abc.txt",ios::in);

f2.open("xyz.txt", ios::in);

char ch;

while(file)

{

f1.get(ch);

if(ch<65 || ch<90)

{

f2.put(ch);

}

return 0;

}

}