Lab Assignment--Binary I/O
Part 1--Writing and Reading Objects
- 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:
- Dump() sets the fields to arbitrary values
- Blank() sets all the fields to empty (NULL, 0, " ", etc)
- Display() shows the contents of each field to the user
- SetData() assigns a fixed value to each field. Choose arbitrary values (different from Dump())
- Output(ofstream&) writes the data to the output stream specified by the argument.
- Input(ifstream&) reads the data from the input stream back into the class' member variables
- 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)!!
- 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?
- 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 .
- Create a code that will read the stringFile.dat. The format of the file is:
streampos [5]
myString
myString
myString
myString
myString
- You can first read the streampos elements into an array
- Cycling five times
- Use seekg and the array of streampos elements (in order) to position the file pointer in the correct position of the file.
- Use the appropriate myString member function to read one object from the file
- Output the line that you've just read
- Don't forget to close the file
Deliverables
- dump.cpp
- dump.h
- compile and run of the code for Part 1
- code for Part 2
- compile and run of the code for Part 2