Table Of Contents
Previous Section Next Section

Program 57: How Not to Read a File

What kind of portability problems exist in the following code?

  1 #include <iostream>
  2
  3 /*
  4  * A data structure consisting of a flag
  5  * which indicates which long int parameter
  6  * follows.
  7  */
  8 struct data
  9 {
 10     // Flag indicating what's to follow
 11     char flag;
 12
 13     // Value of the parameter
 14     long int value;
 15 };
 16
 17 /************************************************
 18  * read_data -- Read data from the given file   *
 19  ************************************************/
 20 void read_data(
 21   std::istream &in_file,     // File to read
 22   struct data &what     // Data to get
 23 )
 24 {
 25     in_file.read(
 26         dynamic_cast<char *>(&what),
 27         sizeof(what));
 28 }

(Next Hint 161. Answer 71.)

Table Of Contents
Previous Section Next Section