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、これには問題ありません。