Module G - File Streams

Binary Access

Input | Output | Comparison | Practice | Exercise



A file object can interpret a file stream using either text or binary format.  Binary format is the access method that preserves data in transfers to and from primary memory.  These transfers do not involve any formatting: that is, any conversion of source values into destination values or any insertion or extraction of record or field separators.  Binary access enables the saving of images of the contents of primary memory, without any loss of information.  The drawback of binary access is that the data is sometimes incomprehensible when viewed in a text file editor. 

Programs that read from and write to binary files are responsible for keeping track of any structure in the file stream.  For instance, the data may consist of records of equal length. 


Input

The header for the member function that reads a file stream under binary access takes the form

 istream& read(char* data, streamsize numberOfBytes) 

data holds the address in memory to where the data is to be transferred.  numberOfBytes holds the numbers of bytes to be transferred from the file stream.  streamsize is an integral type typically defined as a signed long

For example, the following program reads the byte data from the file named on the command line, stores the data in str, and displays the stored string on standard output


 // Binary Access - Reading
 // readBinary.cpp
 // Nov 7 2006

 #include <iostream>
 #include <fstream>
 using namespace std;

 int main(int argc, char* argv[]) {

     char str[1025];
     char* p = str;

     ifstream f(argv[1], ios::in|ios::binary);

     while(f)
         f.read(p++, 1);
     *--p = '\0';

     cout << str << endl;

     return 0;
 }




















 Hello World 



 


Output

The header for the member function that writes to a file stream under binary access takes the form

 ostream& write(const char* data, streamsize numberOfBytes) 

data holds the address in memory from where the data is to be transferred.  numberOfBytes holds the numbers of bytes to be transferred to the file stream. 

For example, the following program writes the string str to the file named on the command line


 // Binary Access - Writing
 // writeBinary.cpp
 // Nov 7 2006

 #include <fstream>
 using namespace std;

 int main(int argc, char* argv[]) {

     char str[] = "Hello World";

     ofstream f(argv[1], ios::out|ios::binary|ios::trunc); 

     f.write(str, sizeof str - 1);

     return 0;
 }


















 


Comparison

The following example compares binary access with text access for the same datum stored in primary memory.  The datum is the floating-point number (1.0/3.0).  Note that the file object discards some precision as it formats the datum under text access.  On the other hand, the file object preserves the original precision under binary access so that the same value is returned to primary memory as originally resided there. 


 // Direct Access
 // binary.cpp
 // Nov 2 2005

 #include <iostream>
 #include <iomanip>
 #include <fstream>
 using namespace std;

 int main(int argc, char* argv[]) {
     double x;

     cout << fixed << setprecision(15);

     // binary access
     fstream f(argv[1],
      ios::in|ios::out|ios::binary|ios::trunc);
     x = 1.0/3.0;
     f.write((char *)&x, sizeof(x));
     f.seekg(0, ios::beg);
     f.read((char *)&x, sizeof(x));
     cout << x << endl;

     // text access
     fstream t(argv[2], ios::in|ios::out|ios::trunc); 
     x = 1.0/3.0;
     t << x;
     t.seekg(0, ios::beg);
     t >> x;
     cout << x << endl;

     return 0;
 }
























 0.333333333333333 






 0.333333000000000


 

When using the read and write functions, it is important to cast the address of the data being transferred to a char* type, so that byte by byte copying can be performed properly. 


In-Class Practice

Try the practice problem in the Handout on Binary Access.


Exercise and Research

  • Read pages 77-84 from Evan Weaver's subject notes; and
  • Read the Wikipedia article on Binary Files.




   Printer Friendly Version of this Page print this page     Top  Go Back to the Top of this Page
Previous Reading  Previous: File Streams Next: Parametric Polymorphism   Next Reading


  Designed by Chris Szalwinski   Copying From This Site