Page 287 - DCAP404 _Object Oriented Programming
P. 287

Object-oriented Programming




                    Notes          Let us write a program that uses get() and put() functions for binary files.
                                   #include  <fstream.h>
                                   void  main()
                                   {
                                          fstream  myfile(“data.dat”,ios::out  |  ios::in  |  ios::binary);
                                                                       //open  file  in  binary  mode

                                          char  ch;
                                          ch=’A’;
                                          myfile.put(ch);              //put  the  content  of  ch  to  the  file
                                          myfile.seekg(ios::beg);      //go  to  the  beginning  of  the  file
                                          myfile.get(ch);              //read  one  character
                                          cout  <<  ch  <<  endl;      //display  it  on  console
                                          myfile.close();

                                   }
                                   The following program uses read() and write() functions for binary file manipulations.
                                   #include  <fstream.h>
                                   #include  <string.h>
                                   void  main()
                                   {
                                          fstream  myfile(“data.dat”,ios::out  |  ios::in  |  ios::binary);
                                          char  Carray[13];
                                          strcpy(Carray,”C++  Programming”); //put  the  string  into  the
                                                                              character  array


                                          myfile.write(Carray,5);      //put  the  first  5  characters  “C++  P”
                                                                       into  the  file
                                          myfile.seekg(ios::beg);      //go  to  the  beginning  of  the  file

                                          static  char  Rarray[10];    //To  store  read  data
                                          myfile.read(Rarray,3);       //read  the  first  3  characters-  “C++”
                                          cout  <<  Rarray  <<  endl;  //display  them  on  console
                                          myfile.close();              //close  file
                                   }




                                      Task
                                     1.   A file in open state file has two pointers associated with it. What are they?

                                     2.   Please quote two methods of opening a data file in a program.




          280                               LOVELY PROFESSIONAL UNIVERSITY
   282   283   284   285   286   287   288   289   290   291   292