title: C++: Is "my text" a std::string, a *char or a c-string?
body: <p>I have just done what appears to be <a href="https://stackoverflow.com/questions/721129/spot-the-error-in-this-file-reading-code-c">a common newbie mistake</a>: </p>
  
  <p>First we read <a href="http://www.learncpp.com/cpp-tutorial/136-basic-file-io/" rel="nofollow noreferrer">one of many tutorials</a> that goes like this: </p>
  
  <pre><code> #include &lt;fstream&gt;
   int main() {
        using namespace std;
        ifstream inf("file.txt");
        // (...)
   }  
  </code></pre>
  
  <p>Secondly, we try to use something similar in our code, which goes something like this:</p>
  
  <pre><code>#include &lt;fstream&gt;
  int main() {
      using namespace std;
      std::string file = "file.txt"; // Or get the name of the file 
                                     // from a function that returns std::string.
      ifstream inf(file);
      // (...)
  }
  </code></pre>
  
  <p>Thirdly, the newbie developer is perplexed by some cryptic compiler error message. </p>
  
  <p>The problem is that <a href="http://www.cplusplus.com/reference/iostream/ifstream/ifstream/" rel="nofollow noreferrer">ifstream</a> takes <code>const * char </code> as a constructor argument.  </p>
  
  <p>The <a href="https://stackoverflow.com/questions/347949/convert-stdstring-to-const-char-or-char">solution</a> is to <a href="http://www.cplusplus.com/reference/string/string/c_str/" rel="nofollow noreferrer">convert std::string to const * char</a>.</p>
  
  <p>Now, the real problem is that, for a newbie, "file.txt" or similar examples given in almost all the tutorials very much looks like a std::string.  </p>
  
  <p>So, is "my text" a std::string, a c-string or a *char, or does it depend on the context?  </p>
  
  <p>Can you provide examples on how "my text" would be interpreted differently according to context?   </p>
  
  <p>[Edit: I thought the example above would have made it obvious, but I should have been more explicit nonetheless: what I mean is the type of any string enclosed within double quotes, i.e. "myfilename.txt", not the meaning of the word 'string'.]</p>
  
  <p>Thanks.</p>