这里有问题,std::cerr << "长度" << passwordLength << std::endl;//如果前文写入的长度是10,此处会输出2573 ,然后输出后文的“2222”,可能是项目其他地方出现问题,可能需要帮我调试一下项目代码,还没写完,不到200行代码,好调试
std::ostream& operator<<(std::ostream& os, const BillManagementSystem& bms) {
// 加密密码
std::string temp = bms.password;
for (auto& i : temp) {
i ^= 6;
}
// 写入密码长度
os.seekp(0,std::ios::beg);
size_t passwordLength = temp.length();
os.write(reinterpret_cast<const char*>(&passwordLength), sizeof(passwordLength));
// 写入密码内容
os.write(temp.c_str(), passwordLength);
}
// 重载输入运算符,用于反序列化
std::istream& operator>>(std::istream& is, BillManagementSystem& bms) {
// 读取密码长度
size_t passwordLength;
is.read(reinterpret_cast<char*>(&passwordLength), sizeof(passwordLength));
if (!is.good()) {
is.setstate(std::ios::failbit); // 设置失败标志
//std::cerr << "11111" << std::endl;
return is;
}
// 读取密码内容
std::string encryptedPassword(passwordLength, '\0');
is.read(reinterpret_cast<char*>(&encryptedPassword[0]), passwordLength);
std::cerr << "长度" << passwordLength << std::endl;//如果前文写入的长度是10,此处会输出2573 ,然后输出后文的“2222”
if (!is.good()) {
is.setstate(std::ios::failbit); // 设置失败标志
std::cerr << "2222" << std::endl;
return is;
}
// 解密密码
for (auto& i : encryptedPassword) {
i ^= 6;
}
bms.password = encryptedPassword;
if (!is.good()) {
is.setstate(std::ios::failbit); // 设置失败标志
std::cerr << "3333" << std::endl;
return is;
}
}