Well you could use a more classic approach of using a mapping in your fileformat (that is, if you have access to the spec (the content) of your files).
You already give the perfect example of what your text should look like:
segment: 2A
name: Aorta_Ascendens
length: 2
radius: 1.47
wall_thickness: .164
young modulus: 4
compliance: 53.4
What's the problem with that format ?
So it gives better flexibility (safety) because you can check for sure that a member is what you expect to read.
You could separate objects with a delimiter:
segment: 2A
name: Aorta_Ascendens
length: 2
radius: 1.47
wall_thickness: .164
young modulus: 4
compliance: 53.4
**@**
segment: 2B
name: Aorta_Ascendens
length: 3
radius: 1.00
wall_thickness: .164
young modulus: 4
compliance: 53.4
Use ifstream.readlines() to get an array of C++ style strings, which will help you parse using find()
function (returns string::npos
if not found).
In the Parse() function it involves iterating over the lines of the file,
first, use find
to look for :
character, takes the left part using substr
to check which member to assign the value (and how to parse, integer, enumeration ?...) and another substr
with a little calculation (eg size - posfound
) to take the right part, and put that into your lexical_cast
of choice. Either boost::lexial_cast
or std::istringstream
.
Otherwise, you have the choice of using boost::serialization
which will let you write only one super simple function to read this kind of formats. (cf annex)
- data binding (to xml, jason, ini...)
Or more complex data binding frameworks like Code Synthesis XSD (Xerces?):
http://www.codesynthesis.com/products/xsd/
And even more simple, use python. you just have to do eval()
PS: Annex (with boost::serialization
) taken from the doc :
class gps_position
{
private:
friend class boost::serialization::access;
// When the class Archive corresponds to an output archive, the
// & operator is defined similar to <<. Likewise, when the class Archive
// is a type of input archive the & operator is defined similar to >>.
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & degrees;
ar & minutes;
ar & seconds;
}
int degrees;
int minutes;
float seconds;
public:
gps_position(){};
gps_position(int d, int m, float s) :
degrees(d), minutes(m), seconds(s)
{}
};
isn't it stupid simple ? and its guaranteed to work. (well as long as you wrote the file with the same serializer).
refer to http://www.boost.org/doc/libs/1_55_0/libs/serialization/doc/index.html