Note that there are some explanatory texts on larger screens.

plurals
  1. POstringstream not working as expected
    primarykey
    data
    text
    <p>I am trying to write program for parsing and processing text file. After not being successful with implementing <strong>sscanf</strong> i decided to try <strong>stringstream</strong>.</p> <p>I have vector of strings containing data separated by spaces, like:</p> <blockquote> <p>some_string another_string yet_another_string VARIABLE_STRING_NO_1 next_string</p> </blockquote> <p>I wrote code and expected result would be:</p> <pre><code>Counter: 4 Variable number 1 : VARIABLE_STRING_NO_1 Variable number 2 : VARIABLE_STRING_NO_2 Variable number 3 : VARIABLE_STRING_NO_3 Variable number 4 : VARIABLE_STRING_NO_4 </code></pre> <p>but instead i get:</p> <pre><code>Counter: 4 Variable number 1 : VARIABLE_STRING_NO_1 Variable number 2 : VARIABLE_STRING_NO_1 Variable number 3 : VARIABLE_STRING_NO_1 Variable number 4 : VARIABLE_STRING_NO_1 </code></pre> <p>Can anyone push me in right direction please? (eg. use other container instead of vector, change method to... etc)</p> <p>Also what if <strong>VARIABLE_STRING</strong> contains 2 sub-strings with space inbetween? That is possible in my data.</p> <p>Sample code:</p> <pre><code>#include &lt;iostream&gt; #include &lt;sstream&gt; #include &lt;vector&gt; using namespace std; int main() { vector&lt;string&gt; vectorOfLines, vectorOfData; vectorOfLines.push_back("some_string another_string yet_another_string VARIABLE_STRING_NO_1 next_string"); vectorOfLines.push_back("some_string another_string yet_another_string VARIABLE_STRING_NO_2 next_string"); vectorOfLines.push_back("some_string another_string yet_another_string VARIABLE_STRING_NO_3 next_string"); vectorOfLines.push_back("some_string another_string yet_another_string VARIABLE_STRING_NO_4 next_string"); string data = "", trash = ""; stringstream token; int counter = 0; for( int i = 0; i &lt; (int)vectorOfLines.size(); i++ ) { token &lt;&lt; vectorOfLines.at(i); token &gt;&gt; trash &gt;&gt; trash &gt;&gt; trash &gt;&gt; data &gt;&gt; trash; vectorOfData.push_back(data); // wrong method here? counter++; // counter to test if for iterates expected times } cout &lt;&lt; "Counter: " &lt;&lt; counter &lt;&lt; endl; for( int i = 0; i &lt; (int)vectorOfData.size(); i++ ) { cout &lt;&lt; "Variable number " &lt;&lt; i + 1 &lt;&lt; " : " &lt;&lt; vectorOfData.at(i) &lt;&lt; endl; } return 0; } </code></pre> <p>Excuse my newbie question but after trying different approaches for last 5 days, I got to the point of swearing and getting discourages to continue learning.<br> Yes I'm very new to C++.<br> I've successfully done the same program in PHP (being total newbie in that too) and it seems like C++ is much, much harder to do.</p>
    singulars
    1. This table or related slice is empty.
    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