Note that there are some explanatory texts on larger screens.

plurals
  1. POReading lines and columns with different separators from a text file
    primarykey
    data
    text
    <p>I am trying to write a function that reads in individual lines from a text file. Each line has two or three columns. I am want to know most elegant/clean approach for it. I am need the function to work with different separators <code>(\t,\n,' ',',',';')</code>.</p> <p>My approach works correctly except for different separators.</p> <p>E.g. Input:</p> <pre><code>6 0 0 1 1 2 2 3 3 4 4 5 5 10 0 1 0.47 2 0 0.67 3 0 0.98 4 0 0.12 2 1 0.94 3 1 0.05 4 1 0.22 3 2 0.24 4 2 0.36 4 3 0.69 </code></pre> <p>Pattern Input:</p> <pre><code>[total number of vertices] [id-vertex][\separetor][name-vertex] ... [total number of edges] [id-vertex][\separator][id-neighbor][\separetor][weight] ... *\separetor=\t|\n|' '|','|';' </code></pre> <p>My approach:</p> <pre><code>void readStream(istream&amp; is, const char separator) { uint n, m; is &gt;&gt; n; cout &lt;&lt; n &lt;&lt; endl; string name; uint vertexId, neighborId; float weight; while(!is.eof()) { for(uint i = 0; i &lt; n; i++) { is &gt;&gt; vertexId &gt;&gt; name; cout &lt;&lt; vertexId; cout &lt;&lt; " " &lt;&lt; name &lt;&lt; endl; } is &gt;&gt; m; cout &lt;&lt; m &lt;&lt; endl; for(uint j = 0; j &lt; n; j++) { is &gt;&gt; vertexId &gt;&gt; neighborId &gt;&gt; weight; cout &lt;&lt; vertexId; cout &lt;&lt; " " &lt;&lt; neighborId; cout &lt;&lt; " " &lt;&lt; weight &lt;&lt; endl; } break; } } </code></pre> <p>Overview:</p> <ol> <li><p>Problem: Different separators.</p></li> <li><p>Others elegantes solutions: In general, someone have other elegant/clean solutions to the problem?</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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