
Originally Posted by
vonCZ
The actual input data (loaded into the program using ifstream) might look like this:
If you load it using ifstream then you can load it directly into doubles and you'll be fine.
Basically what I want to do--regardless of what the input looks like--is be able to set the output so that there will be 2 values right of the decimal... whether the input is float, double, int, long, etc.
You'll have to provide some general mechanism for that. The easiest I can think of is to have several implementations of the same base class that do the reading, parsing and whatever else you may need. Something like (that's a major simplification):
struct MyDataStruct {
float long;
float lat;
};
class Base {
virtual ~Base(){}
virtual bool canHandle(const std::string &format) const = 0;
virtual MyDataStruct getData() = 0;
};
class Doubles : public Base {
Doubles(std::ifstream &input) : Base(), str(input){
}
bool canHandle(const std::string &format) const{ return (format=="DOUBLE"); }
MyDataStruct getData(){
MyDataStruct data;
input >> data.long;
input >> data.lat;
return data;
}
private:
std::ifstream &str;
};
class Strings : public Base {
Strings(std::ifstream &input) : Base(), str(input){}
bool canHandle(const std::string &format) const{ return (format=="TEXT"); }
MyDataStruct getData(){
MyDataStruct data;
std::string str;
input >> str;
data.long = atof(str.c_str());
input >> str;
data.lat = atof(str.c_str());
return data;
}
private:
std::ifstream &str;
};
struct MyDataStruct {
float long;
float lat;
};
class Base {
virtual ~Base(){}
virtual bool canHandle(const std::string &format) const = 0;
virtual MyDataStruct getData() = 0;
};
class Doubles : public Base {
Doubles(std::ifstream &input) : Base(), str(input){
}
bool canHandle(const std::string &format) const{ return (format=="DOUBLE"); }
MyDataStruct getData(){
MyDataStruct data;
input >> data.long;
input >> data.lat;
return data;
}
private:
std::ifstream &str;
};
class Strings : public Base {
Strings(std::ifstream &input) : Base(), str(input){}
bool canHandle(const std::string &format) const{ return (format=="TEXT"); }
MyDataStruct getData(){
MyDataStruct data;
std::string str;
input >> str;
data.long = atof(str.c_str());
input >> str;
data.lat = atof(str.c_str());
return data;
}
private:
std::ifstream &str;
};
To copy to clipboard, switch view to plain text mode
Then you just need to discover what the datatype is (based on some headers or whatever - that's what the "canHandle" method does) and feed the stream into an instance of a proper class.
Bookmarks