How to write std::string to file?
<p>I want to write a std::string variable I am accepting from the user to a file. I tried using the write() method and it writes to the file. But when I open the file I see boxes instead of the string. </p>

<p>The string is only a variable length single word. Is std::string suitable for this or should I use a character array or something.</p>

<pre><code> ofstream write;
 std::string studentName, roll, studentPassword, filename;


public:

void studentRegister()
{
    cout&lt;&lt;"Enter roll number"&lt;&lt;endl;
    cin&gt;&gt;roll;
    cout&lt;&lt;"Enter your name"&lt;&lt;endl;
    cin&gt;&gt;studentName;
    cout&lt;&lt;"Enter password"&lt;&lt;endl;
    cin&gt;&gt;studentPassword;


    filename = roll + ".txt";
    write.open(filename.c_str(), ios::out | ios::binary);

    write.put(ch);
    write.seekp(3, ios::beg);

    write.write((char *)&amp;studentPassword, sizeof(std::string));
    write.close();`
</code></pre>

<p>}</p>
