title: Why does fstream.open(filename) work with literal but not with generated string?
body: <p>In a DLL compiled from C++ with MS Visual Studio 2003, I'm generating a file path by getting a string from SHGetFolderPath and appending a file name, then trying to open that file for output.</p>
  
  <p>I have some globals, one function to setup the file path, and another function to open the file, all shown below.<br>
  The <code>Logger</code> class declaration isn't shown, but it has a member declared as <code>ofstream *ofs</code> which is the file object created in <code>Logger</code>'s constructor.</p>
  
  <p>In <code>constructorBase()</code>, if I call <code>initLogFN()</code> to setup the file path, <code>ofs-&gt;open</code> fails.<br>
  However, if I comment out the <code>initLogFN()</code> call and uncomment the line that sets <code>logPath</code> to a hard-coded string literal, <code>ofs-&gt;open</code> succeeds.</p>
  
  <p>When debugging in either case, Visual Studio correctly shows <code>logPath</code> as having the value "C:\Users\sleis\AppData\Roaming\Address-IT.log" before <code>ofs-&gt;open</code> is called.<br>
  However in the failing case, after the <code>ofs-&gt;open</code> call, <code>logPath</code>'s value changes to six characters that seem random but are the same every time I run my test app.</p>
  
  <p>Why do the two cases behave differently, and how can I fix the failing case?</p>
  
  <pre><code>
  #if defined (WIN32)
  const char dirSep[]="\\";
  #else
  const char dirSep[]="/";
  #endif
  
  const char logFN[]="Address-IT.log";
  char *logPath=0;
  
  bool initLogFN()
  {
      if (logPath!=0) return false;
  
  #if defined (_DEBUG)
  #if defined (WIN32)
      char path[MAX_PATH];
      HRESULT result=SHGetFolderPath(NULL,CSIDL_APPDATA,NULL,SHGFP_TYPE_CURRENT,path);
      if (result!=S_OK) return false;
      strcat(path,dirSep);
      strcat(path,logFN);
      logPath=path;
      return true;
  #endif
  #endif
  
      return false;
  }
  
  void Logger::constructorBase()
  {
      if (!initLogFN()) return;
      //logPath="C:\\Users\\sleis\\AppData\\Roaming\\Address-IT.log";
  
      ofs->open(logPath,ios_base::out | ios_base::app);
  
      if ((ofs->rdstate() & std::ifstream::failbit)!=0) {
          std::cout &lt;&lt; std::endl &lt;&lt; "Error opening log file.";
      } else if (ofs->is_open()) {
          std::cout &lt;&lt; std::endl &lt;&lt; "Log file opened successfully.";
      }
  }
  </code></pre>