title: std::stringstream to read int and strings, from a string
body: <p>I am programming in C++ and I'm not sure how to achieve the following:</p>
  
  <p>I am copying a file stream to memory (because I was asked to, I'd prefer reading from stream), and and then trying to access its values to store them into strings and int variables.</p>
  
  <p>This is to create an interpreter. The code I will try to interpret is (ie):</p>
  
  <pre><code>10 PRINT A
  20 GOTO 10
  </code></pre>
  
  <p>This is just a quick example code. Now the values will be stored in a "map" structure at first and accessed later when everything will be "interpreted".
  The values to be stored are: </p>
  
  <p>int lnum // line number</p>
  
  <p>string cmd // command (PRINT and GOTO)</p>
  
  <p>string exp // expression (A and 10 in this case but could hold expressions like (a*b)-c )</p>
  
  <p>question is given the following code, how do I access those values and store them in memory?
  Also the exp string is of variable size (can be just a variable or an expression) so I am not sure how to read that and store it in the string.</p>
  
  <p>code:</p>
  
  <hr>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;cstdlib&gt;
  #include &lt;cstring&gt;
  #include &lt;map&gt;
  #include &lt;sstream&gt;
  
  using namespace std;
  
   #include "main.hh"
  
  
  int main () 
  {
      int lenght;
      char *buffer;
  
  // get file directory
      string dir;
      cout &lt;&lt; "Please drag and drop here the file to interpret: ";
      getline (cin,dir);
      cout &lt;&lt; "Thank you.\n";
      cout &lt;&lt; "Please wait while your file is being interpreted.\n \n";
  
  // Open File
      ifstream p_prog;
      p_prog.open (dir.c_str());
  
  // Get file size
      p_prog.seekg (0, ios::end);
      lenght = p_prog.tellg();
      p_prog.seekg(0, ios::beg);
  
  // Create buffer and copy stream to it
      buffer = new char[lenght];
      p_prog.read (buffer,lenght);
      p_prog.close();
  
  // Define map&lt;int, char&gt;
      map&lt;int, string&gt; program;
      map&lt;int, string&gt;::iterator iter;
  
  
  /***** Read File *****/
      int lnum; // line number
      string cmd; // store command (goto, let, etc...)
      string exp; // to be subst with expr. type inst.
  
  //  this is what I had in mind but not sure how to use it properly
  //  std::stringstream buffer;
  //  buffer &gt;&gt; lnum &gt;&gt; cmd &gt;&gt; exp;
  
      program [lnum] = cmd; // store values in map
  
  
  
  
  // free memory from buffer, out of scope
      delete[] buffer;
      return 0;
  }
  </code></pre>
  
  <p>I hope this is clear.</p>
  
  <p>Thank you for your help.</p>
  
  <p>Valerio</p>