Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ul> <li>Manual approach</li> </ul> <p>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).</p> <p>You already give the perfect example of what your text should look like:</p> <pre><code>segment: 2A name: Aorta_Ascendens length: 2 radius: 1.47 wall_thickness: .164 young modulus: 4 compliance: 53.4 </code></pre> <p>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:</p> <pre><code>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 </code></pre> <p>Use ifstream.readlines() to get an array of C++ style strings, which will help you parse using <code>find()</code> function (returns <code>string::npos</code> if not found).<br> In the Parse() function it involves iterating over the lines of the file, first, use <code>find</code> to look for <code>:</code> character, takes the left part using <code>substr</code> to check which member to assign the value (and how to parse, integer, enumeration ?...) and another <code>substr</code> with a little calculation (eg <code>size - posfound</code>) to take the right part, and put that into your <code>lexical_cast</code> of choice. Either <code>boost::lexial_cast</code> or <code>std::istringstream</code>.</p> <ul> <li>boost way</li> </ul> <p>Otherwise, you have the choice of using <code>boost::serialization</code> which will let you write only one super simple function to read this kind of formats. (cf annex)</p> <ul> <li>data binding (to xml, jason, ini...)</li> </ul> <p>Or more complex data binding frameworks like Code Synthesis XSD (Xerces?):<br> <a href="http://www.codesynthesis.com/products/xsd/" rel="nofollow">http://www.codesynthesis.com/products/xsd/</a></p> <ul> <li>Python !</li> </ul> <p>And even more simple, use python. you just have to do <code>eval()</code></p> <p>PS: Annex (with <code>boost::serialization</code>) taken from the doc :</p> <pre><code>class gps_position { private: friend class boost::serialization::access; // When the class Archive corresponds to an output archive, the // &amp; operator is defined similar to &lt;&lt;. Likewise, when the class Archive // is a type of input archive the &amp; operator is defined similar to &gt;&gt;. template&lt;class Archive&gt; void serialize(Archive &amp; ar, const unsigned int version) { ar &amp; degrees; ar &amp; minutes; ar &amp; seconds; } int degrees; int minutes; float seconds; public: gps_position(){}; gps_position(int d, int m, float s) : degrees(d), minutes(m), seconds(s) {} }; </code></pre> <p>isn't it stupid simple ? and its guaranteed to work. (well as long as you wrote the file with the same serializer).<br> refer to <a href="http://www.boost.org/doc/libs/1_55_0/libs/serialization/doc/index.html" rel="nofollow">http://www.boost.org/doc/libs/1_55_0/libs/serialization/doc/index.html</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload