c++ - How to write STD:: string to a file?

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.

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

ofstream write;
std::string studentName, roll, studentPassword, filename;


void studentRegister()
    cout<<"Enter roll number"<<endl;
    cout<<"Enter your name"<<endl;
    cout<<"Enter password"<<endl;

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

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

    write.write((char *)&studentPassword, sizeof(std::string));
This question and all comments follow the "Attribution Required."

All Answers

Answers Loading...
Leave a Reply
You must be logged in to post a answer.
About the author