1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <memory>
struct ContactInfo
{
std::string name ;
std::vector<std::string> email ;
ContactInfo() {}
ContactInfo( std::string name_, const std::vector<std::string>& email_ ):name(name_), email(email_) {}
};
namespace{
// utility functions
// I know all these sizes will be < 256. It may not be an assumption you want to make.
unsigned writeSize(std::ofstream& out, unsigned sz)
{
unsigned char size = static_cast<unsigned char>(sz) ;
out.write(reinterpret_cast<char*>(&size), sizeof(size)) ;
return size ;
}
unsigned readSize(std::ifstream&in)
{
unsigned char sz ;
in.read(reinterpret_cast<char*>(&sz), sizeof(sz)) ;
return sz ;
}
}
std::ofstream& Write(std::ofstream& out, const ContactInfo& ci)
{
unsigned size = writeSize(out, ci.name.length()+1) ;
out.write(ci.name.data(), size) ;
size = writeSize(out, ci.email.size()) ;
for( unsigned i=0 ; i < ci.email.size() ; ++i )
{
size = writeSize(out, ci.email[i].length()+1);
out.write(ci.email[i].data(), size) ;
}
return out ;
}
std::ifstream& Read(std::ifstream& in, ContactInfo& ci)
{
unsigned size = readSize(in) ;
std::auto_ptr<char> buffer(new char[256]) ;
in.read(buffer.get(), size) ;
ci.name.assign(buffer.get()) ;
unsigned v_size=readSize(in) ;
ci.email.clear() ;
for ( unsigned i=0; i<v_size; ++i)
{
size=readSize(in) ;
in.read(buffer.get(), size) ;
ci.email.push_back(std::string(buffer.get())) ;
}
return in ;
}
std::ostream& operator <<(std::ostream& out, const ContactInfo& ci )
{
out << "Name: " << ci.name << "\nEmail: " ;
for ( unsigned i=0; i < ci.email.size() ; ++i )
out << ci.email[i] << ' ' ;
return out << '\n' ;
}
std::string email[] =
{ "hobo320@yahoo.com","hobo320@msn.com","train.wreck@gapers.crawl"};
std::string email2[] =
{"pixiedust@hogrider.com","lookatthis@cakefarts.com"};
int main(void)
{
std::vector<ContactInfo> contacts ;
contacts.push_back(ContactInfo("Dwight Eisenhower",
std::vector<std::string>( email, email+(sizeof(email)/sizeof(email[0])) ) )) ;
contacts.push_back(ContactInfo("Henry Hudson",
std::vector<std::string>( email2, email2+(sizeof(email2)/sizeof(email2[0])) ) )) ;
{
std::ofstream out( "ContactInfo.dat", std::ios::out | std::ios::trunc | std::ios::binary ) ;
unsigned c_size = contacts.size() ;
writeSize(out, c_size) ;
for ( unsigned i=0 ; i<contacts.size(); ++i)
Write(out, contacts[i]) ;
}
contacts.clear() ;
{
std::ifstream in( "ContactInfo.dat", std::ios::in | std::ios::binary) ;
contacts.resize(readSize(in)) ;
for ( unsigned i=0; i<contacts.size(); ++i)
Read(in, contacts[i]) ;
}
for ( unsigned i=0; i<contacts.size() ; ++i )
std::cout << contacts[i] << '\n' ;
}
|