ฉันต้องการเขียน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ก็ใช้ได้