Note that there are some explanatory texts on larger screens.

plurals
  1. POc++ reading in text file into vector<vector> then writing to vector or array depending on first word in internal vector
    primarykey
    data
    text
    <p>this is a progression from the question I posted <a href="https://stackoverflow.com/questions/18818777/c-program-for-reading-an-unknown-size-csv-file-filled-only-with-floats-with">c++ program for reading an unknown size csv file (filled only with floats) with constant (but unknown) number of columns into an array</a> . Im now moving onto the real application where I read in a file like:</p> <pre><code>MESH2D MESHNAME "default coverage" NUM_MATERIALS_PER_ELEM 1 E4Q 1 19 20 14 16 2 E4Q 2 17 16 15 23 2 E4Q 3 22 15 14 21 2 E4Q 4 4 3 21 20 1 E4Q 5 6 20 19 7 1 E4Q 6 18 17 10 9 1 E4Q 7 17 23 12 11 1 E4Q 8 7 19 18 8 1 E4Q 9 22 1 13 23 1 E3T 10 14 20 21 2 E3T 11 21 2 22 1 E3T 12 21 3 2 1 E3T 13 22 2 1 1 E3T 14 5 20 6 1 E3T 15 20 5 4 1 E3T 16 16 14 15 2 E3T 17 23 13 12 1 E3T 18 22 23 15 2 E3T 19 17 11 10 1 E3T 20 17 18 16 2 E3T 21 8 18 9 1 E3T 22 18 19 16 2 ND 1 -3.25811078e+002 7.70285567e+001 0.00000000e+000 ND 2 -3.24209146e+002 7.60394871e+001 0.00000000e+000 ND 3 -3.23012110e+002 7.44783503e+001 0.00000000e+000 ND 4 -3.22754089e+002 7.25326647e+001 0.00000000e+000 ND 5 -3.23617358e+002 7.08079432e+001 0.00000000e+000 ND 6 -3.25161538e+002 6.98134116e+001 0.00000000e+000 ND 7 -3.27128620e+002 6.98759747e+001 0.00000000e+000 ND 8 -3.29095703e+002 6.99385378e+001 0.00000000e+000 ND 9 -3.30301095e+002 7.14667646e+001 0.00000000e+000 ND 10 -3.30786908e+002 7.33241555e+001 0.00000000e+000 ND 11 -3.30835733e+002 7.52916270e+001 0.00000000e+000 ND 12 -3.29587322e+002 7.65401204e+001 0.00000000e+000 ND 13 -3.27743000e+002 7.72270000e+001 0.00000000e+000 ND 14 -3.26108525e+002 7.32067724e+001 0.00000000e+000 ND 15 -3.27041416e+002 7.42070316e+001 0.00000000e+000 ND 16 -3.27350377e+002 7.31716751e+001 0.00000000e+000 ND 17 -3.29153676e+002 7.40024406e+001 0.00000000e+000 ND 18 -3.28659180e+002 7.19967464e+001 0.00000000e+000 ND 19 -3.26845856e+002 7.14062637e+001 0.00000000e+000 ND 20 -3.25000347e+002 7.20534611e+001 0.00000000e+000 ND 21 -3.24701329e+002 7.39638966e+001 0.00000000e+000 ND 22 -3.26167714e+002 7.53360591e+001 0.00000000e+000 ND 23 -3.28060316e+002 7.54194849e+001 0.00000000e+000 BEGPARAMDEF GM "Mesh" SI 0 DY 0 TU "" TD 0 0 NUME 3 BCPGC 0 DISP_OPTS entity 0 0 0 0 1 0 0 0 DISP_OPTS inactive 0 0 0 0 1 0 0 0 DISP_OPTS multiple 0 0 0 0 1 0 0 0 BEFONT 0 1 DISP_OPTS entity 1 0 0 0 1 0 0 0 DISP_OPTS inactive 1 0 0 0 1 0 1 0 DISP_OPTS multiple 1 0 0 0 1 0 1 0 BEFONT 1 1 DISP_OPTS entity 2 0 0 0 1 0 0 0 DISP_OPTS inactive 2 0 0 0 1 0 1 0 DISP_OPTS multiple 2 0 0 0 1 0 1 0 BEFONT 2 1 MAT 1 "material 01" MAT 2 "material 02" MAT_MULTI 0 ENDPARAMDEF BEG2DMBC END2DMBC BEGCURVE Version: 1 ENDCURVE </code></pre> <p>called 'example.2dm' and try and write 3 2d vectors (or possibly (preferably) arrays) for the data stored in lines starting with E4Q E3T and ND. I want to use these cell difinitions to print a file with cell centres.</p> <p>I think this sort of problem is very usefull to be documented on here as my last question keeps getting views.</p> <p>My code so far is:</p> <pre><code>// Read in CSV // // Alex Byasse #include &lt;algorithm&gt; #include &lt;fstream&gt; #include &lt;iostream&gt; #include &lt;iterator&gt; #include &lt;sstream&gt; #include &lt;string&gt; #include &lt;vector&gt; #include &lt;cstdlib&gt; enum case_name {e3t, e4q, nd}; case_name checkit(std::string const&amp; inString) { if (inString == "E3T") return e3t; if (inString == "E4Q") return e4q; if (inString == "ND") return nd; } int main() { std::vector&lt;std::vector&lt;std::string&gt;&gt; values; std::ifstream fin("example.2dm"); for (std::string line; std::getline(fin, line); ) { std::istringstream in(line); values.push_back( std::vector&lt;std::string&gt;(std::istream_iterator&lt;std::string&gt;(in), std::istream_iterator&lt;std::string&gt;())); } int nc = 0; int nn = 0; std::vector&lt;std::vector&lt;double&gt;&gt; cells; std::vector&lt;std::vector&lt;double&gt;&gt; nodes; for (int i = 0; i &lt; values.size() - 1; i++) { switch (checkit(values[i][0])){ case e3t: cells[nc].push_back(std::stod(values[i][1])); cells[nc].push_back(std::stod(values[i][2])); cells[nc].push_back(std::stod(values[i][3])); cells[nc].push_back(std::stod(values[i][4])); cells[nc].push_back(std::stod(values[i][2])); cells[nc].push_back(std::stod(values[i][5])); nc++; break; case e4q: cells[nc].push_back(std::stod(values[i][1])); cells[nc].push_back(std::stod(values[i][2])); cells[nc].push_back(std::stod(values[i][3])); cells[nc].push_back(std::stod(values[i][4])); cells[nc].push_back(std::stod(values[i][5])); cells[nc].push_back(std::stod(values[i][6])); nc++; break; case nd: nodes[nn].push_back(std::stod(values[i][1])); nodes[nn].push_back(std::stod(values[i][2])); nodes[nn].push_back(std::stod(values[i][3])); nn++; break; } } } </code></pre> <p>Can I get some help fixing this code and also. Is this correct?</p> <pre><code>#include &lt;cstdlib&gt; </code></pre> <p>Im getting the following compilation error:</p> <pre><code>$ g++ read_csv2.cpp -std=c++0x read_csv2.cpp: In function ‘int main()’: read_csv2.cpp:44:26: error: ‘stod’ is not a member of ‘std’ read_csv2.cpp:45:33: error: ‘stod’ is not a member of ‘std’ read_csv2.cpp:46:26: error: ‘stod’ is not a member of ‘std’ read_csv2.cpp:47:33: error: ‘stod’ is not a member of ‘std’ read_csv2.cpp:48:33: error: ‘stod’ is not a member of ‘std’ read_csv2.cpp:49:33: error: ‘stod’ is not a member of ‘std’ read_csv2.cpp:53:26: error: ‘stod’ is not a member of ‘std’ read_csv2.cpp:54:26: error: ‘stod’ is not a member of ‘std’ read_csv2.cpp:55:26: error: ‘stod’ is not a member of ‘std’ read_csv2.cpp:56:26: error: ‘stod’ is not a member of ‘std’ read_csv2.cpp:57:26: error: ‘stod’ is not a member of ‘std’ read_csv2.cpp:58:26: error: ‘stod’ is not a member of ‘std’ read_csv2.cpp:62:26: error: ‘stod’ is not a member of ‘std’ read_csv2.cpp:63:26: error: ‘stod’ is not a member of ‘std’ read_csv2.cpp:64:26: error: ‘stod’ is not a member of ‘std’ </code></pre> <p>I'm running g++ on cygwin. I've checked the man pages and -std=c++11 is an available option and I've tryed it yet I get the same error?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

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