Page 291 - DCAP404 _Object Oriented Programming
P. 291

Object-oriented Programming




                    Notes                 for(int  j=0;  j<4;j++)
                                          myfile.get(ch);
                                          myfile.seekg(-2);
                                          //this  will  take  the  pointer  to  2  characters  before  the  current
                                   location.  It  will  now  point  to  (l)
                                   seekp()

                                   This function does to output files what seekp() does to input files. It shifts the pointer to the
                                   specified location in the output file. If you want to overwrite the last 5 characters, you will have
                                   to go back 5 characters from the current pointer position. This you can do by the following
                                   statement.
                                   myfile.seekp(-5);

                                   ignore()

                                   This function is used when reading a file to ignore certain number of characters. You can use
                                   seekg() as well for this purpose  just to  move the pointer up  in the file. However,  ignore()
                                   function has one advantage over seekg() function. The prototype is of ignore() function is given
                                   below.

                                   fstream& ignore( int, char);
                                   Caution
                                   You can specify a delimiter character in ignore() function whence it ignores all the characters up
                                   to the first occurrence of the specified delimiter.
                                   Where int is the count of characters to be ignored and delimiter is the character up to which you
                                   would like to ignore as demonstrated in the following program.

                                   //demonstration  of  ignore()  function
                                   #include  <fstream.h>
                                   void  main()
                                   {
                                                 //Assume  that  the  text  contained  in  data.dat  file  is  “Welcome
                                   to  C++”
                                                 ifstream  myfile(“data.dat”);
                                                 static  char  Carray[10];
                                                 //go  on  ignoring  all  the  characters  in  the  input  up  to  10th
                                   character  unless  an               ‘m’  is  found

                                                 myfile.ignore(10,’m’);
                                                 myfile.read(Carray,10);
                                                 cout  <<  Carray  <<  endl;  //it  should  display  “me  to  C++”
                                                 myfile.close();
                                   }







          284                               LOVELY PROFESSIONAL UNIVERSITY
   286   287   288   289   290   291   292   293   294   295   296