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이것에 대한 괜찮습니다.