Lab Assignment--Binary I/O


Part 1--Writing and Reading Objects

  1. Create a dump.h and dump.cpp file for the following class:
    #define MAXSTRLEN 5
    
    class Dump
    {
      public:
            Dump(); // default constructor
            void Blank();
            void Display();
            void SetData();
            void Input(ifstream&);
            void Output(ofstream&);
      private:
            int integer1;
            int integer2;
            double double1;
            double double2;
            char c_str1[MAXSTRLEN];
            char c_str2[MAXSTRLEN];
            string cpp_str1;
            string cpp_str2;
    };
    Where:
  2. The main code is available through the following copy command:
    cp /net/data/ftp/pub/class/330/BinaryIO/Exercise/main.cpp .
    Do not alter this code (specifically the order of the calls)!!

  3. As an experiment, try reading and writing the entire object. To help you out, your code in the Input function will look something like this:
    infile.read((char*)this, sizeof(*this));

    What happens? How can you fix it?

  4. Make sure that all the data is printing. That is the code to show your lab instructor.

Part 2--Finding a Message

Your goal in this code is to use seekg to print the correct ordering of a five line message. The message has been written into a file in random order. Preceding the message in the file is an array of five file pointers (of type streampos) to help you rearrange the message.

To prepare for this exercise you can copy the following files:

cp /net/data/ftp/pub/class/330/BinaryIO/Exercise/stringFile.dat .
cp /net/data/ftp/pub/class/330/BinaryIO/Exercise/myString.cpp .
cp /net/data/ftp/pub/class/330/BinaryIO/Exercise/myString.h .

  1. Create a code that will read the stringFile.dat. The format of the file is:
    streampos [5]
    myString
    myString
    myString
    myString
    myString
  2. Cycling five times
    1. Use seekg and the array of streampos elements (in order) to position the file pointer in the correct position of the file.

    2. Use the appropriate myString member function to read one object from the file

    3. Output the line that you've just read

  3. Don't forget to close the file

Deliverables