For this assignment, you will write two classes,
IFSerial
and OFSerial
. They will behave much like ifstream
and ofstream
, except that they will read & write serialized data.
                
Your classes must have the folowing operators & methods:                 
IFSerial
:
std::string
filename
IFSerial >> bool
IFSerial >> int
IFSerial >> short
IFSerial >> long
IFSerial >> char
IFSerial >> std::string
IFSerial.eof()
IFSerial.fail()
OFSerial
:
std::string
filename
OFSerial << bool
OFSerial << int
OFSerial << short
OFSerial << long
OFSerial << char
OFSerial << std::string
OFSerial.fail()
The above may be methods, or free functions, as you wish.                 
Const-correctness, both arguments & methods, is your job. For example,
it must be possible to call .eof()
on a const IFSerial
.
                
You may add other functions, methods, or operators, public or private, as you wish.                 
.eof()
                .eof()
will return true
if and only if end-of-file has been
encountered. It reflects the past; it does not predict the future.
.fail()
                .fail()
will return true
if and only if a conversion
has failed, or if the given filename can’t be opened in the
desired manner.
IFSerial
or OFSerial
in boolean context yields
the opposite boolean result than .fail()
.
FSerial.h
                FSerial.h
will contain class declarations
for both IFSerial
and OFSerial
.
FSerial.h
and any necessary code in your hw7.tar
.
cout
or cerr
, throw
anything, or exit.
.fail()
should return true
.
Here is a sample test program:                 
% cat test.cc #include "FSerial.h" #include <fstream> #include <iostream> #include <string> #include <cassert> using namespace std; int main() { cout << "Test begins.\n"; { OFSerial out("data"); out << true; out << 'x' << 42; } IFSerial in("data"); bool b; char c; int i; in >> b >> c; in >> i; assert(b); assert(c == 'x'); assert(i == 42); assert(in); assert(!in.fail()); assert(!in.eof()); in >> i; assert(!in); // same as in.fail() assert(in.fail()); // a conversion (in >> i) failed assert(in.eof()); // we hit end-of-file // Read the raw datafile ifstream raw("data"); string data; while (raw.get(c)) data += c; assert(data == "t" "cx" "i\x10\x2a"); // true, 'x', 42 // Try writing to a file that can’t be created. OFSerial o("/this/does/not/exist"); assert(!o); assert(o.fail()); o << 6488063; // Must not complain, throw, or exit. assert(!o); assert(o.fail()); cout << "Test ends.\n"; return 0; } % g++ -Wall test.cc hw7.a % ./a.out Test begins. Test ends.
If you encounter “STACK FRAME LINK OVERFLOW”, then try this:
export STACK_FRAME_LINK_OVERRIDE=ffff-ad921d60486366258809553a3db49a4a
Requirements are not inherited, except as noted below:
<<
or >>
must work in the usual I/O stream manner.
hw7.a
.
<stdio.h>
or <cstdio>
facilities,
such as printf
, scanf
, fopen
, and getchar
.
cout
, and ifstream
.
hw7.a
.
main()
function
to test your code. Don’t turn it in.
hw7.tar
*.cc
)
*.h
), including FSerial.h
Makefile
, with a capital M
)
Makefile
’s default target must create the library file
hw7.a
.
Makefile
must use at least -Wall
when compiling.
~cs253/bin/checkin HW7 hw7.tar
Turn in someone else’s work.                 
User: Guest