title: How to convert vector to string and convert back to vector
body: <p>----------------- EDIT -----------------------</p>
  
  <p>Based on juanchopanza's comment : I edit the title</p>
  
  <p>Based on jrok's comment : I'm using ofstream to write, and ifstream to read.</p>
  
  <p>I'm writing 2 programs, first program do the following tasks :</p>
  
  <ol>
  <li>Has a vector of integers</li>
  <li>convert it into array of string </li>
  <li>write it in a file</li>
  </ol>
  
  <p>The code of the first program :</p>
  
  <pre><code>vector&lt;int&gt; v = {10, 200, 3000, 40000};
  int i;
  stringstream sw;
  string stringword;
  
  cout &lt;&lt; "Original vector = ";
  for (i=0;i&lt;v.size();i++) 
  {
       cout &lt;&lt; v.at(i) &lt;&lt; " " ;
  }
  cout &lt;&lt; endl;
  
  for (i=0;i&lt;v.size();i++) 
  {
      sw &lt;&lt; v[i];
  }
  stringword = sw.str();
  cout &lt;&lt; "Vector in array of string : "&lt;&lt; stringword &lt;&lt; endl;
  
  ofstream myfile;
  myfile.open ("writtentext");
  myfile &lt;&lt; stringword;
  myfile.close();
  </code></pre>
  
  <p>The output of the first program :</p>
  
  <pre><code>Original vector : 10 200 3000 40000
  Vector in string : 10200300040000
  Writing to File ..... 
  </code></pre>
  
  <p>second program will do the following tasks :</p>
  
  <ol>
  <li>read the file</li>
  <li>convert the array of string back into original vector</li>
  </ol>
  
  <p>----------------- EDIT -----------------------</p>
  
  <p>Now the writing and reading is fine, thanks to Shark and Jrok,I am using a comma as a separator. The output of first program :</p>
  
  <pre><code>Vector in string : 10,200,3000,40000,
  </code></pre>
  
  <p>Then I wrote the rest of 2nd program :</p>
  
  <pre><code>string stringword;
  
  ifstream myfile;
  myfile.open ("writtentext");
  getline (myfile,stringword);
  cout &lt;&lt; "Read From File = " &lt;&lt; stringword &lt;&lt; endl;
  
  cout &lt;&lt; "Convert back to vector = " ;
  for (int i=0;i&lt;stringword.length();i++)
  {
      if (stringword.find(','))
      {
          int value;
          istringstream (stringword) &gt;&gt; value;
          v.push_back(value);
          stringword.erase(0, stringword.find(','));
      }
  }
  for (int j=0;j&lt;v.size();i++) 
  {
      cout &lt;&lt; v.at(i) &lt;&lt; " " ;
  }
  </code></pre>
  
  <p>But it can only convert and push back the first element, the rest is erased. Here is the output :</p>
  
  <pre><code>Read From File = 10,200,3000,40000,
  Convert back to vector = 10
  </code></pre>
  
  <p>What did I do wrong? Thanks</p>