最佳答案
我想写一个 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();`
}