Добавление в репозиторий HTML в C ++ проблема - PullRequest
0 голосов
/ 28 апреля 2020

Я реализовал репозиторий 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, но мне действительно нужна помощь, пожалуйста.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...