Tôi muốn ghi một std::stringbiến mà tôi đang chấp nhận từ người dùng vào một tệp. Tôi đã thử sử dụng write()phương pháp này và nó ghi vào tệp. Nhưng khi tôi mở tệp, tôi thấy các hộp thay vì chuỗi.
Chuỗi chỉ là một từ đơn có độ dài thay đổi. Có std::stringphù hợp cho điều này hay tôi nên sử dụng một mảng ký tự hoặc một cái gì đó.
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::stringthì tốt cho điều này.