#include #include #include using namespace std; #include "prompt.h" #include "date.h" // illustrates reading/writing raw bits, binary files int main() { string filename = PromptString("file for storing Dates: "); int limit = PromptRange("# of Dates ",10,10000); Date today; int start = today.Absolute(); string text = filename + ".txt"; string binary = filename + ".bin"; cout << "testing program on " << today << endl; ofstream toutput(text.c_str()); // open text file int k; for(k=start; k < start+limit; k++) // write text form of dates { toutput << Date(k) << endl; } toutput.close(); // open binary file, write raw dates ofstream boutput(binary.c_str(),ios_base::binary); for(k=start; k < start+limit; k++) { Date d(k); boutput.write(reinterpret_cast(&d),sizeof(d)); } boutput.close(); // open input file to read raw dates from ifstream input(binary.c_str(),ios_base::binary); input.seekg(0,ios_base::beg); // to the beginning streampos startp= input.tellg(); // position of start input.seekg(0,ios_base::end); // seek to end of stream streampos endp = input.tellg(); // position of end int size = endp-startp; // number of entries cout << "size of file: " << size << ", # dates = " << size/sizeof(Date) << endl; // read alldates in file, start at front input.seekg(0, ios_base::beg); for(k=0; k < size/sizeof(Date); k++) { input.read(reinterpret_cast(&today),sizeof(today)); cout << today << endl; } return 0; }