File Handling


A file is a collection of related data stored in a particular area on the disk . The data is stored in disk using the concept of file .

Stream Classes


  • Filebuf :- its purpose is to set the file buffer to read and write . Contain openprot constant used in the open() of file stream classes . Also contain close() and open() as method.


  • Fstreambase :- provides operations common to the file stream. Serves as a base for fstream, ifstream and ofsteram class. Contains open() and close() function


  • Ifstream :- provides input operations. Contains open() with default input mode. Inherits the functions get(), getline(), read(), seekg() and tellg() function from istream.


  • Ofstream :- provides output operations. Contains open() with default output mode. Inherits put(), seekp(), teelp() and write() function from ostream.


  • Fsream :- provides support for simultaneous input and output operations. Contains open() with default input mode. Inherits all the function from isteram and ostream classes through iostream

Modes of Opening


  
  ios :: out = open file for write only
  ios :: in   = open file for read only
  ios :: app = append to end-of-file
  ios :: ate  = take us to the end of the file when it   is opened
                    
  Both ios :: app and ios :: ate take us to the end of the file 
 
  when it is 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 to modify the existing data any where in the file.
  The mode can combine two or more parameters 
 
  using the bitwise OR operator (symbol |)
eg :-              
fstream file;

file . Open(“ data . txt”, ios :: out | ios :: in);

Manipulations of File Pointers


    

    When we want to move file pointer to desired 
    position then use these function for manage the 
    file pointers. 
                  
1.  Seekg ()  = moves get pointer (input) to a 
                      specified location
2. Seekp ()  = moves put pointer (output) to a 
                      specified location
3. tellg ()     = gives the current position of the get pointer 

4.  tellp ()     = gives the current position of the put pointer 
   ex.
  
   fout . seekg(0, ios :: beg)  --     go to start
    fout . seekg(0, ios :: cur)   --     stay at current position   
     fout . seekg(0, ios :: end)  --     go to the end of file
    fout . seekg(m, ios :: beg) --   move to m+1 byte in the file
     fout . seekg(m, ios :: cur)  --    go forward by m bytes from   
                        the current position
    fout . seekg(-m, ios :: cur) --    go backward by m bytes    
                         from the current position
    fout . seekg(-m, ios :: end) --   go backward by m bytes 
                        from the end
 
                                          

Example


 
  #include< iostream . h>
  #include< conio .h>
  #include< fstream . h>
  
  Class student
  {
        Public:
        Struct  stu
        {
          char name[20];
          int roll;
        }s;
     Void put_data();
     Void get_data();
   };
   void student :: get_data()
   {
      int  temp;
      cout<<"enter roll no. ";
      cin >>temp;
      fstream file; 
      file. open ("hit . txt", ios :: in);
      file.seekg(0,ios::beg);
      While (file . read ((char *) this, sizeof (student)))
       {
         If (temp==s. roll)
          {
              cout<<"student name "<< s . name<<"\n";
              cout<<"student roll "<< s . roll;
           }
        }
       getch ();
     }
     void main()
     {
       clrscr();
       student st;
       st .put_data();
     }