0

我已经为我的项目实现了一个 HTML 存储库,但我有一个问题,当我添加一些东西时,这里只有最后一个元素。我也必须在没有缓存内存的情况下这样做。这是我的代码的外观:

HTMLRepository::HTMLRepository(const std::string& fileName)
{
    this->fileName = fileName;
}

bool HTMLRepository::addDog(const Dog& dog)
{
    //if (this->findDog(dog.getName()) != -1)
    //  throw RepositoryException("Dog already exists");
    vector<Dog> dogList = this->getAllDogs();
    dogList.push_back(dog);
    this->writeVectorToFile(dogList);
    return true;
}

这就是我写它们的方式:

void HTMLRepository::writeVectorToFile(std::vector<Dog> vectorOfDogs)
{
    fstream fileOut(this->fileName);
    fileOut << "<!DOCTYPE html>" << endl;
    fileOut << "<html>" << endl;
    fileOut << "<head>" << endl;
    fileOut << "<title>myDogs</title>" << endl;
    fileOut << "</head>" << endl;
    fileOut << "<body>" << endl;
    fileOut << "<table border = '1'>" << endl;
    fileOut << "<tr>" << endl;
    fileOut << "<td>Name</td>" << endl;
    fileOut << "<td>Breed</td>" << endl;
    fileOut << "<td>Birth Date</td>" << endl;
    fileOut << "<td>Number of shots</td>" << endl;
    fileOut << "<td>Photo</td>" << endl;
    fileOut << "</tr>" << endl;
    for (auto dog : vectorOfDogs) {
        fileOut << "<tr>" << endl;
        fileOut << "<td>" << dog.getName() << "</td>" << endl;
        fileOut << "<td>" << dog.getBreed() << "</td>" << endl;
        fileOut << "<td>" << dog.getBirthDate() << "</td>" << endl;
        fileOut << "<td>" << dog.getNumberOfShots() << "</td>" << endl;
        fileOut << "<td>" << dog.getPhoto() << "</td>" << endl;
        fileOut << "</tr>" << endl;
    }
    fileOut << "</table>" << endl;
    fileOut << "</body>" << endl;
    fileOut << "</html>" << endl;
    fileOut.close();
}

这就是我获取所有对象的方式:

std::vector<Dog> HTMLRepository::getAllDogs() const
{
    fstream fileInput(this->fileName);
    int numberOfUselessLines = 14, position;
    string line, name, breed, birth, photo;
    int numberOfShots;
    while (numberOfUselessLines != 0)
    {
        numberOfUselessLines--;
        getline(fileInput, line);
    }
    Dog dog;
    vector<Dog> vectorOfDogs;
    while (getline(fileInput, line))
    {
        if (line.find("table>"))
            break;
        getline(fileInput, line);
        position = line.find(">");
        line.erase(0, position + 1);
        position = line.find("<");
        name = line.substr(0, position);

        getline(fileInput, line);
        position = line.find(">");
        line.erase(0, position + 1);
        position = line.find("<");
        breed = line.substr(0, position);

        getline(fileInput, line);
        position = line.find(">");
        line.erase(0, position + 1);
        position = line.find("<");
        birth = line.substr(0, position);

        getline(fileInput, line);
        position = line.find(">");
        line.erase(0, position + 1);
        position = line.find("<");
        breed = line.substr(0, position);

        getline(fileInput, line);
        position = line.find(">");
        line.erase(0, position + 1);
        position = line.find("<");
        numberOfShots = stoi(line.substr(0, position));

        getline(fileInput, line);
        position = line.find(">");
        line.erase(0, position + 1);
        position = line.find("<");
        photo = line.substr(0, position);

        dog = Dog(name, breed, birth, numberOfShots, photo);
        vectorOfDogs.push_back(dog);
        getline(fileInput, line);
    }
    fileInput.close();
    return vectorOfDogs;
}

我知道这些函数很长,因为它适用于 HTML,但我真的需要一些帮助,拜托。

4

0 回答 0