Page 286 - DCAP404 _Object Oriented Programming
P. 286

Unit 13: Working with Files




          myfile.seekg(10) //Take the pointer to 10 characters after the current location       Notes
          myfile.seekg(-10) //Take the pointer to 10 characters before the current location
          Moreover, the function seekg()is overloaded to take two parameters. Thus, another form of the
          function is,
          myfile.seekg(-4, ios::end) //Take the pointer 4 characters before the end of file
          Most of the time it is necessary to check whether a specified file exists or not while opening it.
          Opening of file may also fail due to other reasons. Remember that if the file opening fails the
          open() function return a non-zero value. By checking the return type of the function one can
          ensure whether the file was opened successfully or not as demonstrated in the following program
          snippet.
          fstream  myfile(“data.dat”);

          if  (!myfile)
          {
          cout  <<  “Error  :  File  could  not  be  opened\n”;
          exit(1);
          }
          The ofstream class also provides a fail() function which return true if the opening of file operation
          failed as shown in the following code snippet
          ofstream  myfile(“data.dat”,  ios::nocreate);
          if(myfile.fail())
          {
          cout  <<  “Error  :  File  could  not  be  opened\n”;

          exit(1);
          }
          binary files are unformatted and uninterpreted file of binary digits. It simply contains binary
          numbers whose meaning is provided by the program that manipulates the file contents. The
          functions that give you the possibility to write/read unformatted files are get() and put(). To
          read a byte, you can use get() and to write a byte, use put().  Both get() and put() functions take
          one parameter - a char variable or character.
          If you want to read/write whole blocks of data, then you can use the read() and write() functions.
          Their prototypes are:

          istream &read(char *buf, streamsize num);
          ostream &write(const char *buf, streamsize num);
          For the read() function, but should be an array of chars, where the read block of data will be put.
          For the write() function, buf is an array of chars, where is the data you want to save in the file. For
          the both functions, num is a number, that defines the amount of data (in symbols) to be read/
          written.
          Another function that provides the number of symbols read so far - gcount(). It simply function
          returns the number of read symbols for the last unformatted input operation. You can specify
          that the file is going to be operated on in binary mode in the open() function. The required mode
          is ios::binary.





                                           LOVELY PROFESSIONAL UNIVERSITY                                   279
   281   282   283   284   285   286   287   288   289   290   291