Full Details of IO and File Management in object oriented programming.

 Full Details of IO and File Management in object oriented programming.

1] Explain C++ stream classes.




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.

                        Secondary function


  • 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;
          }

3] List out and explain functions and manipulators used for Formatted I/O opetrations.
  • We can format input and output by following methods.
  1. ios class functions 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:
  • In set() we can provide one or two argument.
  • 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.

  • The flags without field bit are shown below.

  • 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<<"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 are used to manipulate the output in specific format.
  • Example for manipulators:
          #include<iostream>
          #include<iomanip>
          using namespace std;
          {
               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)<<resretiosflags (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

4] Explain file stream classes with iostream classes in short.



5] List out various File mode parameters and explain in short.
  • 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 if stream implies input and creating a stream using of stream 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.
6] Explain File pointing function in short.
  • Each file has two pointers one is getpointer to input and second one is putpointer to output.
  • Function for manipulation of file 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.
👉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("sahil",1);
                    e[1]=emp("yash",2);
                    e[2]=emp("abhi",3);
                    e[3]=emp("dhruv",4);

                    fstream file;
                    file.open("employee.data", ios::in | ios::out);

                    int i;
                    for(i=0; i<4;i++)
                    file.write((char 9*) &e[i], size of (e[i]));
                    file.seekg(0, ios::end);
                    int end=file.tellg();

                    cout<<"number of objecs stored in employee.data is"
                           <<size of (emp);

7] W.A.P. 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;
          }

8] W.A.P. that write content of file1 in file2 but omits uppercase 
     latters. 
 
          #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;
                      }
          }                                       
         

               
               

Comments