title: Getting char* array from c++ stringstream ostringstream
body: <p>I am trying to copy <strong>ostringstream</strong> to a <strong>char*</strong> array and am hoping someone can help me with understanding where my mistake lies.  I looked on the forum, found some things that are similar and unfortunately am still am unable to get property copy from ostringstream to char*.  In short I am trying to copy into the char* via:</p>
  
  <pre><code>ostringstream bld
  bld&lt;&lt;"test"&lt;&lt;"is"&lt;&lt;"good"
  const char * result = bld.str().c_str();
  </code></pre>
  
  <p>The complete code to reproduce an error is below.  In this code I am having two functions that essentially build strings via ostringstream.  In the <strong>makeFilePath(</strong>) function I build a complete file path (ie <strong>/path/file.txt</strong>).  In the <strong>add()</strong> function, I simply add two more char* arrays to an argument to get <em>prefix /path/file.txt suffix</em>.  </p>
  
  <p>The problem is for some unknown reason to me the <em>fullFilePath</em> also changes to look like <em>prefix /path/file.txt suffix</em>.  Last three lines of a code will exhibit that.  </p>
  
  <p>I spent hours on this, thinking maybe this is a referencing issue or something else.  However, none that I attemped worked.  Any ideas how to get over this problem?  </p>
  
  <p>Thanks!!</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;sstream&gt;
  #include &lt;string&gt;
  
  using std::cout;
  using std::endl;
  using std::string;
  
  using std::ostringstream;
  
  const char* add(const char *fileName) {
      ostringstream bld;
      const char *prefix = "prefix";
      const char *suffix = "suffix";
      bld &lt;&lt; prefix &lt;&lt; " " &lt;&lt; fileName &lt;&lt; " " &lt;&lt; suffix;
      string temp = bld.str();
      cout &lt;&lt; "in add(): \n\t" &lt;&lt; temp &lt;&lt; endl;
      return temp.c_str();
  }
  
  const char * makeFilePath(const char *path, const char *name, const char *ext =
          ".txt") {
      ostringstream bld;
      bld &lt;&lt; path &lt;&lt; name &lt;&lt; ext;
      cout &lt;&lt; "makeFilePath(), returning: \n\t" &lt;&lt; bld.str()&lt;&lt; endl;
      string temp = bld.str();
      return temp.c_str();
  
  }
  
  int main(int argc, char **argv) {
      cout &lt;&lt; "=== PROJECT START ===" &lt;&lt; endl;
  
      const char * filePath = "\\Path\\";
      const char *fileName = "FILENAME";
      const char *fullFilePath = makeFilePath(filePath, fileName);
  
      cout &lt;&lt; fullFilePath before calling add():\n\t" &lt;&lt; fullFilePath &lt;&lt; endl;    
      const char* str = add(fullFilePath);
      cout &lt;&lt; fullFilePath after calling add():\n\t" &lt;&lt; fullFilePath &lt;&lt; endl;
  
      return 1;
  }
  </code></pre>