How can I stop my C++ from appending multiple times to html?

I am trying to create a portfolio for my JavaScript projects. And in order to display the last write time of each index file, I use C++ and write directly to the file. However, because of how I wrote the code, the file that is read and written to is the same. So basically, if I compile my code once, the write time is displayed once. If compiled 2 times, the time is displayed twice, etc.

Question: How can I fix my C++ so that my last_write_time is always displayed once?

index.html:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE-edge">
        <meta name="viewport", content="width=device-width, initial-scale=1.0">
        <title>My Portfolio!</title>
    </head>

    <body>
        <div id="navigation-bar">
            <a href="index.html">Home</a>
        </div>

        <div id="main-display">
            <div id="games-display">
                <h1>Games</h1>
                <a href="games.html"><img src="./Images/Games Display Background.jpg"></a>
            </div>

            <div id="non-games-display">
                <h1>Non-Games</h1>
                <a href="non-games.html"><img src="./Images/Non-Games Display Background.jpg"></a>
            </div>    
        </div>   
    </body>
</html>

non-games.html:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE-edge">
        <meta name="viewport", content="width=device-width, initial-scale=1.0">
        <title>Non-Games</title>
    </head>

    <body>
        <div id="navigation-bar">
            <a href="index.html">Home</a>
        </div>

        <div id="non-games" style="width: 100vw; height: 100vh; font-size: 18pt; font-family: Arial, Helvetica, sans-serif; margin-left: 15px;">
            <a href="../JavaScript Non-Games/Analog Clock/index.html">Analog Clock</a><br>
            <a href="../JavaScript Non-Games/Bouncy Balls/Bouncy Balls.html">Bouncy Balls</a><br>
            <a href="../JavaScript Non-Games/Brick Layer/index.html">Brick Layer</a><br>
            <a href="../JavaScript Non-Games/Checklist/index.html">Checklist</a><br>
            <a href="../JavaScript Non-Games/DateToDay/index.html">Convert date to weekday</a><br>
            <a href="../JavaScript Non-Games/First Quiz/First Quiz.html">First Quiz</a><br>
            <a href="../JavaScript Non-Games/Growing Squares/index.html">Growing Squares</a><br>
            <a href="../JavaScript Non-Games/Jukebox remastered/Jukebox.html">Jukebox</a><br>
            <a href="../JavaScript Non-Games/Mouse Grid/index.html">Mouse Grid</a><br>
            <a href="../JavaScript Non-Games/My first graphic/Good canvas.htm">My First Graphic</a><br>
            <a href="../JavaScript Non-Games/NewTime/index.html">Convert a Current Time to a New Time</a><br>
            <a href="../JavaScript Non-Games/Paint Splatter/index.html">Paint Splatter</a><br>
            <a href="../JavaScript Non-Games/Random Group Assignment (Matt's version)/index.html">Random Group Assignment</a><br>
            <a href="../JavaScript Non-Games/Random number/Random number.htm">Random Number Generator</a><br>
            <a href="../JavaScript Non-Games/Snake Eyes/Snake Eyes.html">Snake Eyes</a><br>
            <a href="../JavaScript Non-Games/StopWatch/index.html">Stop Watch</a><br>
            <a href="../JavaScript Non-Games/TimeDifference/index.html">Length Between Two Times</a><br>
        </div>

        <script></script>
    </body>
</html>

games.html:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE-edge">
        <meta name="viewport", content="width=device-width, initial-scale=1.0">
        <title>Games</title>
    </head>

    <body>
        <div id="navigation-bar">
            <a href="index.html">Home</a>
        </div>

        <div id="games" style="width: 100vw; height: 100vh; font-size: 18pt; font-family: Arial, Helvetica, sans-serif; margin-left: 15px; border-right: 1px solid black;">
            <a href="../JavaScript Games/Cookie Clicker game/index.html">Cookie Clicker</a><br>
            <a href="../JavaScript Games/Fighting Game/Fighting Game.html">Fighting Game</a><br>
            <a href="../JavaScript Games/Guess That Word!/index.html">Guess That Word!</a><br>
            <a href="../JavaScript Games/Rock Paper Scissors/Rock Paper Scissors Game.htm">Rock Paper Scissors</a><br>
            <a href="../JavaScript Games/Whac-a-mole/Whac-a-mole.htm">Whac-a-Mole</a><br>
        </div>  

        <script></script>
    </body>
</html>

main.cpp:

#include <iostream>
#include <filesystem>
#include <fstream>
#include <chrono>
#include <string>
#include <sstream>
#include <iomanip>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif

namespace fs = std::filesystem;

std::string format_time(fs::file_time_type ftime) {
    auto sctp = std::chrono::time_point_cast<std::chrono::system_clock::duration>(
        ftime - fs::file_time_type::clock::now() + std::chrono::system_clock::now()
    );
    std::time_t cftime = std::chrono::system_clock::to_time_t(sctp);
    std::tm* tm = std::localtime(&cftime);
    std::ostringstream oss;
    oss << std::put_time(tm, "%A %B %d, %Y At %H:%M:%S");
    return oss.str();
}

void update_html(const std::string& html_path, const std::string& base_dir) {
    std::ifstream infile(html_path);
    std::ostringstream buffer;
    buffer << infile.rdbuf();
    std::string html_content = buffer.str();
    infile.close();

    std::string updated_content;
    std::istringstream iss(html_content);
    std::string line;
    while (std::getline(iss, line)) {
        size_t pos = line.find("<a href="");
        if (pos != std::string::npos) {
            size_t start = pos + 9; // Skip '<a href="'
            size_t end = line.find(""", start);
            std::string relative_path = line.substr(start, end - start);
            fs::path filepath = fs::path(base_dir) / relative_path;

            if (fs::exists(filepath)) {
                std::string last_write_time = format_time(fs::last_write_time(filepath));
                size_t close_tag_pos = line.find("</a>", end);
                if (close_tag_pos != std::string::npos) {
                    line.insert(close_tag_pos, " (Last Updated On " + last_write_time + ")");
                }
            }
        }
        updated_content += line + "n";
    }

    std::ofstream outfile(html_path);
    outfile << updated_content;
    outfile.close();
}

void open_html(const std::string& file_path) {
    std::string command;
#ifdef _WIN32
    command = "start " + file_path;
#else
    command = "xdg-open " + file_path;
#endif
    system(command.c_str());
}

int main() {
    std::string games_html = "games.html";
    std::string non_games_html = "non-games.html";
    std::string games_dir = "../JavaScript Games";
    std::string non_games_dir = "../JavaScript Non-Games";

    update_html(games_html, games_dir);
    update_html(non_games_html, non_games_dir);

    open_html("index.html");

    return 0;
}

I have tried to make a temp file, but I didn’t know how to do it correctly. Based on how my code is written, I’m not quite sure how to do it. I was going to try and just use C++ to write each file, but I wanted to try to make this work first.

An example of 2 compilations:
Whac-a-mole (Last Updated On DATE At TIME)(Last Updated On DATE At TIME)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật