Page 285 - DCAP404 _Object Oriented Programming
P. 285

Object-oriented Programming




                    Notes                 ofstream  myfile(“data1.dat”,  ios::ate);
                                          myfile  <<  “Save  this  text  to  the  file”;
                                          myfile.close();
                                   }
                                   This program will write Save this text to the file at the end of the file data1.dat without removing
                                   the previous content of the file.
                                   A file can also be opened in mixed mode using OR (|) operator  to combine  the modes. For
                                   example,

                                   ios::ate | ios::binary
                                   opens the file in binary mode for output at the current pointer position without removing the
                                   previous contents of the file. Another example of mixed mode is given below.

                                   fstream myfile(“data.dat”,ios::in | ios::out);
                                   Input file stream (ifstream) allows only reading from the file and output file stream (ofstream)
                                   only for writing into the file. If you wish to open a file both for writing and reading at the same
                                   time you should use file stream (fstream) with specific opening mode as the following program
                                   demonstrates.

                                   #include  <fstream.h>
                                   void  main()
                                   {
                                          fstream  myfile  (“data.dat”,ios::in  |  ios::out);
                                          myfile  <<  “Write  this  text”;    //Writing  into  the  file
                                          static  char  TextRead[50];         //Create an array to hold what is

                                                                              read  from  the  file
                                          myfile.seekg(ios::beg);             //Get  back  to  the  beginning  of
                                                                              the  file  explained  later
                                          myfile  >>  TextRead;
                                          cout  <<  TeaxtRaed  <<  endl;
                                          myfile.close();
                                   }
                                   In this program the statement fstream myfile (“data.dat”,ios::in | ios::out); creates an object
                                   from class fstream. At the time of execution, the program opens the file data.dat in read/write
                                   mode (ios::in | ios::out) which allows to read from the file, and put data into it, at the same time.
                                   This is done by allowing the program to move the character pointer in the file to a desired
                                   location before reading or writing.
                                   The read  and write operation takes  place on the current position of the pointer  in the  file.
                                   Therefore you might need  to shift this position  to a  desired location.  The member function
                                   seekg() of the class fstream allows you to do just that as shown below.
                                   myfile.seekg(ios::beg)  //Take the pointer to the beginning of the file
                                   myfile.seekg(ios::end)  //Take the pointer to the end of the file






          278                               LOVELY PROFESSIONAL UNIVERSITY
   280   281   282   283   284   285   286   287   288   289   290