我想std::string将用户接受的变量写到文件中。我尝试使用该write()方法并将其写入文件。但是,当我打开文件时,看到的是框而不是字符串。
该字符串只是一个可变长度的单词。是否std::string适合这个还是应该使用字符数组或东西。
ofstream write;
std::string studentName, roll, studentPassword, filename;
public:
void studentRegister()
{
cout<<"Enter roll number"<<endl;
cin>>roll;
cout<<"Enter your name"<<endl;
cin>>studentName;
cout<<"Enter password"<<endl;
cin>>studentPassword;
filename = roll + ".txt";
write.open(filename.c_str(), ios::out | ios::binary);
write.put(ch);
write.seekp(3, ios::beg);
write.write((char *)&studentPassword, sizeof(std::string));
write.close();`
}
std::string就可以做到这一点。