少了accountInfo.length()< 5的情况
时间: 2024-04-21 14:24:00 浏览: 15
非常抱歉,我忽略了检查`accountInfo`列表长度小于5的情况。在此修正一下:
```cpp
void AccountInfo::addFriend(const QString& accountName, const QString& newFriend)
{
QString fileName = "accountInfo.txt";
QFile file(fileName);
if (file.open(QIODevice::ReadWrite | QIODevice::Text)) {
QTextStream stream(&file);
QStringList lines;
while (!stream.atEnd()) {
QString line = stream.readLine();
QStringList accountInfo = line.split(",");
if (accountInfo.length() >= 5 && accountInfo[0] == accountName) {
// 添加新朋友到好友列表
QStringList friendList = accountInfo[4].split(";");
friendList.append(newFriend);
accountInfo[4] = friendList.join(";");
line = accountInfo.join(",");
}
lines.append(line);
}
file.resize(0);
for (const QString& line : lines) {
stream << line << endl;
}
file.close();
}
qDebug() << "Add friend success";
}
```
现在它会检查`accountInfo`列表的长度是否大于等于5,以避免处理不完整的账户信息。感谢您的指正!