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
|
template <typename T>
void write(const std::ostream &stream,const T &datum){
stream <<datum1<<std::endl;
}
//Note: Don't extrapolate this to use many parameters. More than 3, and it's probably time to pass a structure.
template <typename T1,typename T2>
void write(const std::string &name,const T1 &datum1,const T2 &datum2){
std::ofstream file(name.c_str());
if (!file.is_open())
return;
write(file,datum1);
write(file,datum2);
}
template <typename T>
void read(const std::istream &stream,const T &datum){
stream >>datum;
//I'm not entirely sure if the above line leaves a newline in the input
//buffer. If it does, use the code below, instead:
std::string line;
std::getline(stream,line);
std::stringstream s(line);
s >>datum;
}
//I've used template specialization one twice before, so the syntax may be a
//little off. I think it's right, though.
template <>
void read <std::string>(const std::istream &stream,const T &datum){
std::string line;
std::getline(stream,line);
datum=line;
}
template <typename T1,typename T2>
void read(const std::string &name,const T1 &datum1,const T2 &datum2){
std::ifstream file(name.c_str());
if (!file.is_open())
return;
read(file,datum1);
read(file,datum2);
}
|