std::string
변수를 사용자가 파일로 수락하려고합니다. 내가 write()
메서드를 사용하여 시도하고 파일에 씁니다. 그러나 파일을 열 때 문자열 대신 상자가 나타납니다.std :: string을 파일에 쓰는 방법은 무엇입니까?
문자열은 가변 길이 단일 단어입니다. 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();`
}
코드를 표시하십시오. 일반적으로 _if_가 올바르게 사용 되었기 때문에'std :: string'이 이에 적합합니다. – Useless
문자열 객체의 'payload'CONTENT를 저장해야합니다. (일반적으로 길이와 실제 내용을 가리키는 포인터를 포함합니다) –